简体   繁体   English

位移位,右移4位

[英]bit shifiting, shifting right 4 bits

so I'm doing bit shifting for C 所以我正在为C做位移

00001010 >> 4 shouldn't give 0000000? 00001010 >> 4不应该给0000000?

because I'm shifting the bit to 4 times right, which is same as dividing by 4. but on my notes it says 10101000 is the notes wrong? 因为我将位右移4次,这与除以4相同。但是在我的音符上它说10101000音符错误吗?

Your notes are wrong. 你的笔记错了。 printf("%d\\n", strtol("00001010", 0, 2) >> 4) produces “0”. printf("%d\\n", strtol("00001010", 0, 2) >> 4)产生“ 0”。

右移 00001010 bin(10月10日)4次将确实给您:00000000 bin(0 dec),而左移 4次:00001010 bin(10月10日)将给您:10100000 bin(160 dec 编辑

Each shift is the same as dividing the value by 2. Shifting to the right four times is like dividing by 2 four times. 每次移位都等于将值除以2。向右移位四次就像将四分之二。 It's integer math so any fractional portions are truncated. 这是整数运算,因此所有小数部分都会被截断。

Assuming 00001010 (10) is binary notation, the results are as follows: 假设00001010 (10)是二进制表示法,结果如下:

Shift 1: 00000101 (5) Shift 1: 00000101 (5)
Shift 2: 00000010 (2) Shift 2: 00000010 (2)
Shift 3: 00000001 (1) Shift 3: 00000001 (1)
Shift 4: 00000000 (0) Shift 4: 00000000 (0)

So the result is indeed 0. 因此结果确实为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM