操作字符变量的位

发布于 2024-10-28 05:02:41 字数 427 浏览 0 评论 0原文

我的目标是通过为每个位分配值来构建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南渊 2024-11-04 05:02:41

这里没有问题,输出是正确的。

原因是:

当您使用 |= 设置 packet 的值时,该值为 10,十进制表示为 2。 (1 << x) 到 bit,您实际上分配的是值 2(二进制的 10)。

维基百科条目:

判断第二位是否为
1、按位与对其应用并且
另一个包含 1 的位模式
第二位:

    0011 (decimal 3) 
AND 0010 (decimal 2)   
  = 0010 (decimal 2)

如果您的目的只是检查布尔值是否已设置该位,只需将其转换为布尔值即可。

(希望一切都有意义,我有点累了;))

There's no problem here, the output is correct.

Here's the reason:

When you set the value of packet with |=, the value is 10 which, in decimal, is 2. When you assign packet & (1 << x) to bit, you're actually assigning the value 2 (10 in binary).

Wikipedia entry:

To determine whether the second bit is
1, a bitwise AND is applied to it and
another bit pattern containing 1 in
the second bit:

    0011 (decimal 3) 
AND 0010 (decimal 2)   
  = 0010 (decimal 2)

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 ;))

稳稳的幸福 2024-11-04 05:02:41

首先:

char packet;
packet &= ~(1 << 0);

这是不可靠的,因为 packet 从内存中最后的内容(即垃圾)开始。 <代码>1 << 0 只是 1~1 是 ...111110。使用packet每次都会给出不同的答案,具体取决于内存中最后的内容;唯一确定的是最后一位(即最低有效位)将被设置为0

packet |= (1 << 1);

这只是将第二位设置为1。所以现在packetxxxxxx10

然后你的循环会遍历前两位;每个位都用packet & 进行掩码。 (1 << x),但这掩盖了它,它不会移动它。因此,在第一次迭代期间,1 << x1,您得到第一位 (0)。第二次迭代,1 << x10,即十进制的 2。将 xxxxxx1010 相结合得到 10,您可以立即打印出来(它的格式显示为 2)。

如果您想移动该位(以便将其隔离为 0 或 1),您可以使用:

bit = (packet & (1 << x)) >> x;

或等效但更具可读性

bit = (packet >> x) & 1;

这将产生(我假设需要)01 而不是 02

First thing:

char packet;
packet &= ~(1 << 0);

This is not reliable, since packet starts off with whatever was in memory last (i.e. garbage). 1 << 0 is just 1. ~1 is ...111110. Anding that with packet 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 to 0.

packet |= (1 << 1);

This just sets the second bit to 1. So now packet is xxxxxx10.

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 is 1, and you get the first bit (0). The second iteration, 1 << x is 10, or 2 in decimal. Anding xxxxxx10 with 10 gives 10, which you promptly print out (it appears formatted as 2).

If you want to move the bit (in order to isolate it as a 0 or 1), you can use:

bit = (packet & (1 << x)) >> x;

Or the equivalent but more readable

bit = (packet >> x) & 1;

This will yield the (I'm assuming desired) output of 0, 1 instead of 0, 2.

指尖凝香 2024-11-04 05:02:41

该输出似乎完全符合预期。位 0 未设置,位 1 已设置。也许您正在追求

bit = !!(packet & (1 << x));

...如果您想获得 01 结果。

顺便说一句,您应该初始化packet - 使用未初始化的变量会导致未定义的行为。

That output appears entirely expected. Bit 0 isn't set, Bit 1 is set. Perhaps you're after

bit = !!(packet & (1 << x));

...if you want to get an 0 or 1 result.

By the way, you should initialise packet - use of an uninitialised variable results in undefined behaviour.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文