什么按位移所有8位hapens [英] What hapens with bitwise shift for all 8 bits
问题描述
我在C小的查询,
我使用上留下数69移按位是 01000101
二进制
I have a small query in c,
I am using the bitwise left shift on number 69 which is 01000101
in binary
01000101 << 8
和我得到的答案 1000101亿
难道不应该是全部8 zeors即 00000000
当我们转移所有的8位零离开,然后垫。
Shouldn't it be all 8 zeors i.e. 00000000
as we shift all the 8 bits to left and then pad with zeros.
推荐答案
这是因为文字(默认数据类型)的对数( INT
),在大多数现今的CPU,大于的 8位
(典型值 32位
),因此,当你申请
It is because of the literal (default data type) for a number (int
) is, in most of nowadays CPU, greater than 8-bit
(typically 32-bit
) and thus when you apply
69 << 8 //note 69 is int
据实际应用这样
00000000 00000000 00000000 01000101 << 8
因此,你得到的结果。
Thus you get the result
00000000 00000000 01000101 00000000
如果您使用的,比方说, unsigned char型
明确,那么就不会发生:
If you use, say, unsigned char
specifically, then it won't happen:
unsigned char a = 69 << 8; //resulting in 0
这是因为,虽然 69 LT;&LT; 8
本身仍然导致
This is because though 69 << 8
itself will still result in
01000101 00000000
但上面的值将被强制转换为 8位
unsigned char型
,结果:
00000000
这篇关于什么按位移所有8位hapens的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!