程序员面试宝典纠错,取反操作的优先级高于移位,而非移位的优先级高于取反,整型提升蒙蔽了真相 1 //测试移位操作符与取反操作符的优先级 2 void TestShift() 3 { 4 //对unsigned char类型左移 5 cout<<“对unsigned char类型左移”<<endl; 6 unsigned char a = 0xa5; 7 unsigned char b = ~a>>4; 8 unsigned char c = (~a)>>4; 9 unsigned char d = ~(a>>4); 10 unsigned char e = ~a; 11 unsigned char f = e>>4; 12 13 cout<<(int)a<<endl<<(int)b<<endl<<(int)c<<endl<<(int)d<<endl<<(int)e<<endl<<(int)f<<endl; 14 15 //对unsigned char类型右移 16 cout<<“对unsigned char类型右移”<<endl; 17 unsigned char a0 = 0xa5; 18 unsigned char b0 = ~a0<<4; 19 unsigned char c0 = (~a0)<<4; 20 unsigned char d0 = ~(a0<<4); 21 unsigned char e0 = ~a0; 22 unsigned char f0 = e0<<4; 23 24 cout<<(int)a0<<endl<<(int)b0<<endl<<(int)c0<<endl<<(int)d0<<endl<<(int)e0<<endl<<(int)f0<<endl; 25 26 //对int类型,符号位为0的情况 27 int a1 = 0xa7; 28 int b1 = ~a1>>4; 29 int c1 = (~a1)>>4; 30 int d1 = ~(a1>>4); 31 int e1 = ~a1; 32 int f1 = e1>>4; 33 34 cout<<“a1 = “<<a1<<” b1 = “<<b1<<” c1 = “<<c1<<” d1 = “<<d1<<” e1 = “<<e1<<” f1 = “<<f1<<endl; 35 36 cout<<“a1 = “; 37 PrintBinaryOfInt(a1); 38 cout<<“b1 = “; 39 PrintBinaryOfInt(b1); 40 cout<<“c1 = “; 41 PrintBinaryOfInt(c1); 42 cout<<“d1 = “; 43 PrintBinaryOfInt(d1); 44 cout<<“e1 = “; 45 PrintBinaryOfInt(e1); 46 cout<<“f1 = “; 47 PrintBinaryOfInt(f1); 48 49 //对int类型,符号位为1的情况 50 int a2 = 0xa; 51 int b2 = ~a2>>4; 52 int c2 = (~a2)>>4; 53 int d2 = ~(a2>>4); 54 int e2 = ~a2; 55 int f2 = e2>>4; 56 57 cout<<“a2 = “<<a2<<” b2 = “<<b2<<” c2 = “<<c2<<” d2 = “<<d2<<” e2 = “<<e2<<” f2 = “<<f2<<endl; 58 59 cout<<“a2 = “; 60 PrintBinaryOfInt(a2); 61 cout<<“b2 = “; 62 PrintBinaryOfInt(b2); 63 cout<<“c2 = “; 64 PrintBinaryOfInt(c2); 65 cout<<“d2 = “; 66 PrintBinaryOfInt(d2); 67 cout<<“e2 = “; 68 PrintBinaryOfInt(e2); 69 cout<<“f2 = “; 70 PrintBinaryOfInt(f2); 71 72 //对int类型,测试右移操作符 73 int a3 = 0xa7; 74 int b3 = ~a3<<4; 75 int c3 = (~a3)<<4; 76 int d3 = ~(a3<<4); 77 int e3 = ~a3; 78 int f3 = e3<<4; 79 80 cout<<“a3 = “<<a3<<” b3 = “<<b3<<” c3 = “<<c3<<” d3 = “<<d3<<” e3 = “<<e3<<” f3 = “<<f3<<endl; 81 82 cout<<“a3 = “; 83 PrintBinaryOfInt(a3); 84 cout<<“b3 = “; 85 PrintBinaryOfInt(b3); 86 cout<<“c3 = “; 87 PrintBinaryOfInt(c3); 88 cout<<“d3 = “; 89 PrintBinaryOfInt(d3); 90 cout<<“e3 = “; 91 PrintBinaryOfInt(e3); 92 cout<<“f3 = “; 93 PrintBinaryOfInt(f3); 94 95 //对unsigned char类型左移 96 cout<<“对unsigned char类型左移”<<endl; 97 unsigned char a4 = 0xa5; 98 unsigned char b4 = ~a4>>(unsigned char)4; 99 unsigned char c4 = (~a4)>>(unsigned char)4; 100 unsigned char d4 = ~(a4>>(unsigned char)4); 101 unsigned char e4 = ~a4; 102 unsigned char f4 = e4>>(unsigned char)4; 103 104 cout<<(int)a4<<endl<<(int)b4<<endl<<(int)c4<<endl<<(int)d4<<endl<<(int)e4<<endl<<(int)f4<<endl; 105 106 }
2024最新激活全家桶教程,稳定运行到2099年,请移步至置顶文章:https://sigusoft.com/99576.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。 文章由激活谷谷主-小谷整理,转载请注明出处:https://sigusoft.com/89839.html