移位

来源:互联网 发布:任天堂游戏机淘宝 编辑:程序博客网 时间:2024/05/26 19:16

C++(无逻辑移位 >>> <<<)

右移符号补齐(不过是有符号数还是无符号数),

左移0补齐, 

(对于无符号数的操作都是逻辑的  )

例子如下:

// right shift [padding : positive: high bit 0] [negative: high bit 1]int a1 = 0xffff;  //positive: high bit 0int b1 = a1 >> 4; //0x00000fffint a2 = 0xf0ffffff; //negative: high bit 1int b2 = a2 >> 4; //0xff0fffffunsigned int ua3 = 0xf0ffffff;unsigned int ub3 = ua3 >> 4; //0x0f0fffff//left shift [padding: always 0]int a4 = 0xf0000fff;int b4 = a4 << 4; //0x0000fff0int a5 = 0xf0ffffff;int b5 = a5 << 8; //0xffffff00int a6 = 0x0fffffff;int b6 = a6 << 4; //0xfffffff0unsigned int ua7 = 0xf0000fff;unsigned int ub7 = ua7 << 4; //0x0000fff0ub7 = ua7 << 1; //0xe0001ffe

Java (即有算术移位,又有逻辑移位)

逻辑右移:

 (-127)10 >>> 2 = (11111111111111111111111110000001)2 >>> 2 = (00111111111111111111111111100000)2 =(1073741792)10

算术右移:

(-127)10 >> 2 = (11111111111111111111111110000001)2 >> 2 = (11111111111111111111111111100000)2 = (-32)10


0 0
原创粉丝点击