c int:
1 #include<stdio.h> 2 main(){ 3 //int 4 int j=1; //<<31==2147483648 5 // 1<<32==1 6 // (1<<32)-1==0 7 // (1<<32)-2==4294967295 8 for(int i=30;i<(1<<5)+1;i++) 9 printf("1<<%d==%ld\n",i,j<<i); 10 printf("(1<<%d)-1==%ld\n",32,(j<<32)-1); 11 printf("(1<<%d)-2==%ld\n",32,(j<<32)-2); 12 }View Code
1<<30==1073741824
1<<31==2147483648
1<<32==1
(1<<32)-1==0
(1<<32)-2==4294967295
c unsigned int:
1 #include<stdio.h> 2 main(){ 3 //unsigned int 4 unsigned int j=1;//1<<31==2147483648 5 // 1<<32==1 6 // (1<<32)-1==0 7 // (1<<32)-2==4294967295 8 for(int i=30;i<(1<<5)+1;i++) 9 printf("1<<%d==%ld\n",i,j<<i); 10 printf("(1<<%d)-1==%ld\n",32,(j<<32)-1); 11 printf("(1<<%d)-2==%ld\n",32,(j<<32)-2); 12 }View Code
1<<30==1073741824
1<<31==2147483648
1<<32==1
(1<<32)-1==0
(1<<32)-2==4294967295
c long:
1 #include<stdio.h> 2 main(){ 3 //long 4 long j=1;//1<<63==-9223372036854775808 5 // 1<<64==1 6 // (1<<63)-1==9223372036854775807 7 // (1<<64)-1==0 8 // (1<<64)-2==-1 9 for(int i=62;i<(1<<6)+1;i++) 10 printf("1<<%d==%ld\n",i,j<<i); 11 printf("(1<<%d)-1==%ld\n",63,(j<<63)-1); 12 printf("(1<<%d)-1==%ld\n",64,(j<<64)-1); 13 printf("(1<<%d)-2==%ld\n",64,(j<<64)-2); 14 }View Code
1<<62==4611686018427387904
1<<63==-9223372036854775808
1<<64==1
(1<<63)-1==9223372036854775807
(1<<64)-1==0
(1<<64)-2==-1
c unsigned long:
1 #include<stdio.h> 2 main(){ 3 // unsigned long 4 unsigned long j=1;//1<<63==-9223372036854775808 5 // 1<<64==1 6 // (1<<63)-1==9223372036854775807 7 // (1<<64)-1==0 8 // (1<<64)-2==-1 9 for(int i=62;i<(1<<6)+1;i++) 10 printf("1<<%d==%ld\n",i,j<<i); 11 printf("(1<<%d)-1==%ld\n",63,(j<<63)-1); 12 printf("(1<<%d)-1==%ld\n",64,(j<<64)-1); 13 printf("(1<<%d)-2==%ld\n",64,(j<<64)-2); 14 }View Code
1<<62==4611686018427387904
1<<63==-9223372036854775808
1<<64==1
(1<<63)-1==9223372036854775807
(1<<64)-1==0
(1<<64)-2==-1
c完整测试代码:
1 #include<stdio.h> 2 main(){ 3 //int 4 printf("int:\n"); 5 int j=1; //<<31==2147483648 6 // 1<<32==1 7 // (1<<32)-1==0 8 // (1<<32)-2==4294967295 9 for(int i=30;i<(1<<5)+1;i++) 10 printf("1<<%d==%ld\n",i,j<<i); 11 printf("(1<<%d)-1==%ld\n",32,(j<<32)-1); 12 printf("(1<<%d)-2==%ld\n",32,(j<<32)-2); 13 // unsigned int 14 printf("\nunsigned int:\n"); 15 unsigned int j2=1;//1<<31==2147483648 16 // 1<<32==1 17 // (1<<32)-1==0 18 // (1<<32)-2==4294967295 19 for(int i=30;i<(1<<5)+1;i++) 20 printf("1<<%d==%ld\n",i,j2<<i); 21 printf("(1<<%d)-1==%ld\n",32,(j2<<32)-1); 22 printf("(1<<%d)-2==%ld\n",32,(j2<<32)-2); 23 //long 24 printf("\nlong:\n"); 25 long j3=1;//1<<63==-9223372036854775808 26 // 1<<64==1 27 // (1<<63)-1==9223372036854775807 28 // (1<<64)-1==0 29 // (1<<64)-2==-1 30 for(int i=62;i<(1<<6)+1;i++) 31 printf("1<<%d==%ld\n",i,j3<<i); 32 printf("(1<<%d)-1==%ld\n",63,(j3<<63)-1); 33 printf("(1<<%d)-1==%ld\n",64,(j3<<64)-1); 34 printf("(1<<%d)-2==%ld\n",64,(j3<<64)-2); 35 // unsigned long 36 printf("\nunsigned long:\n"); 37 unsigned long j4=1;//1<<63==-9223372036854775808 38 // 1<<64==1 39 // (1<<63)-1==9223372036854775807 40 // (1<<64)-1==0 41 // (1<<64)-2==-1 42 for(int i=62;i<(1<<6)+1;i++) 43 printf("1<<%d==%ld\n",i,j4<<i); 44 printf("(1<<%d)-1==%ld\n",63,(j4<<63)-1); 45 printf("(1<<%d)-1==%ld\n",64,(j4<<64)-1); 46 printf("(1<<%d)-2==%ld\n",64,(j4<<64)-2); 47 }View Code
int:
1<<30==1073741824
1<<31==2147483648
1<<32==1
(1<<32)-1==0
(1<<32)-2==4294967295
unsigned int:
1<<30==1073741824
1<<31==2147483648
1<<32==1
(1<<32)-1==0
(1<<32)-2==4294967295
long:
1<<62==4611686018427387904
1<<63==-9223372036854775808
1<<64==1
(1<<63)-1==9223372036854775807
(1<<64)-1==0
(1<<64)-2==-1
unsigned long:
1<<62==4611686018427387904
1<<63==-9223372036854775808
1<<64==1
(1<<63)-1==9223372036854775807
(1<<64)-1==0
(1<<64)-2==-1
c++如果输出用printf则所有结果与c相同。
下面是用std::cout<<;的输出方式的测试
c++完整测试代码:
标签:main,各种类型,int,左移,unsigned,c++,111,long,include From: https://www.cnblogs.com/xuweihui/p/17417445.html