操作字符变量的位
我的目标是通过为每个位分配值来构建 char 变量,即我需要为每个位分配 0 和 1。
我执行了以下代码:
char packet;
int bit;
packet &= ~(1 << 0);
packet |= (1 << 1);
printf("\n Checking each bit of packet: \n");
for(int x=0;x<2;x++)
{
bit = packet & (1 << x);
printf("\nBit [%d] of packet : %d", x, bit);
}
但我得到的输出是:
Bit[0] of packet : 0 Bit[1] of packet : 2
这里有什么问题?
My objective is to frame a char variable by assigning values to each bit, i.e. I need to assign 0's and 1's to each bit.
I did the following code:
char packet;
int bit;
packet &= ~(1 << 0);
packet |= (1 << 1);
printf("\n Checking each bit of packet: \n");
for(int x=0;x<2;x++)
{
bit = packet & (1 << x);
printf("\nBit [%d] of packet : %d", x, bit);
}
But the output I am getting is:
Bit[0] of packet : 0 Bit[1] of packet : 2
What is the problem here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里没有问题,输出是正确的。
原因是:
当您使用
|=
设置 packet 的值时,该值为10
,十进制表示为 2。 (1 << x) 到bit
,您实际上分配的是值 2(二进制的10
)。维基百科条目:
如果您的目的只是检查布尔值是否已设置该位,只需将其转换为布尔值即可。
(希望一切都有意义,我有点累了;))
There's no problem here, the output is correct.
Here's the reason:
When you set the value of packet with
|=
, the value is10
which, in decimal, is 2. When you assignpacket & (1 << x)
tobit
, you're actually assigning the value 2 (10
in binary).Wikipedia entry:
If your intent is to simply check a boolean value of whether or not the bit has been set, simply cast it to a bool value.
(Hope that all made sense, I'm a little tired atm ;))
首先:
这是不可靠的,因为
packet
从内存中最后的内容(即垃圾)开始。 <代码>1 << 0 只是1
。~1
是 ...111110。使用packet
每次都会给出不同的答案,具体取决于内存中最后的内容;唯一确定的是最后一位(即最低有效位)将被设置为0
。这只是将第二位设置为
1
。所以现在packet
是xxxxxx10
。然后你的循环会遍历前两位;每个位都用
packet & 进行掩码。 (1 << x)
,但这仅掩盖了它,它不会移动它。因此,在第一次迭代期间,1 << x
是1
,您得到第一位 (0
)。第二次迭代,1 << x
是10
,即十进制的 2。将xxxxxx10
与10
相结合得到10
,您可以立即打印出来(它的格式显示为2
)。如果您想移动该位(以便将其隔离为 0 或 1),您可以使用:
或等效但更具可读性
这将产生(我假设需要)
0
、1
而不是0
、2
。First thing:
This is not reliable, since
packet
starts off with whatever was in memory last (i.e. garbage).1 << 0
is just1
.~1
is ...111110. Anding that withpacket
will give a different answer each time, depending on what was in memory last; the only sure thing is that the last bit (i.e. least significant) will be set to0
.This just sets the second bit to
1
. So nowpacket
isxxxxxx10
.Your loop then goes over the first two bits; each bit is masked with
packet & (1 << x)
, but that only masks it, it does not move it around. So during the first iteration,1 << x
is1
, and you get the first bit (0
). The second iteration,1 << x
is10
, or 2 in decimal. Andingxxxxxx10
with10
gives10
, which you promptly print out (it appears formatted as2
).If you want to move the bit (in order to isolate it as a 0 or 1), you can use:
Or the equivalent but more readable
This will yield the (I'm assuming desired) output of
0
,1
instead of0
,2
.该输出似乎完全符合预期。位 0 未设置,位 1 已设置。也许您正在追求
...如果您想获得
0
或1
结果。顺便说一句,您应该初始化
packet
- 使用未初始化的变量会导致未定义的行为。That output appears entirely expected. Bit 0 isn't set, Bit 1 is set. Perhaps you're after
...if you want to get an
0
or1
result.By the way, you should initialise
packet
- use of an uninitialised variable results in undefined behaviour.