简体   繁体   English

如何在不使用bitset的情况下显示unsigned int中的位?

[英]How do I display the bits in an unsigned int WITHOUT using bitset?

I am currently working on a project for school covering bit manipulation. 我目前正在研究一个涵盖位操作的学校项目。 We are supposed to show the bits for an unsigned integer variable and allow the user to manipulate them, turning them on and off and shifting them. 我们应该显示无符号整数变量的位,并允许用户操纵它们,打开和关闭它们并移动它们。 I have all of the functionality working, except for displaying the bits once they have been manipulated. 除了在操作后显示位,我才能使用所有功能。 We are NOT allowed to use bitset to display the bits, and it will result in a heavy grade reduction. 我们不允许使用bitset来显示位,这将导致严重的等级降低。

I have tried using if statements to determine whether the bits are on or off, but this does not seem to be working. 我已经尝试使用if语句来确定这些位是打开还是关闭,但这似乎不起作用。 Whenever a bit is changed, it will simply print a lot of 0's and 1's. 每当有一点改变时,它只会打印很多0和1。

std::cout << "Bits: ";

for (int i = sizeof(int)*8; i > 0; i--)
{

    if (a | (0 << i) == 1)
        std::cout << 1;
    if (a | (0 << i) == 0)
        std::cout << 0;

}

std::cout << std::endl << a;

I would expect that if I turn a bit on, that one bit will display a 1 instead of a 0 , with the rest of the bits being unchanged and still displaying 0 ; 我希望如果我开启一点,那一位将显示1而不是0 ,其余的位保持不变并仍然显示0 ; instead it prints a string of 1010101 about the length of half the console. 相反,它打印一个1010101的字符串,大约是控制台一半的长度。

There are a couple of problems here, and you might want to do a detailed review of bit manipulation: 这里有几个问题,您可能想要对位操作进行详细的审查:

  • for (int i = sizeof(int)*8; i > 0; i--) should be for (int i = sizeof(int)*8 - 1; i >= 0; i--) , because bits are 0-indexed (shifting 1 to the left 0 times gives a set bit on the rightmost position). for (int i = sizeof(int)*8; i > 0; i--)for (int i = sizeof(int)*8 - 1; i >= 0; i--) ,因为位为0 -indexed(向左移动1次,在最右边的位置给出一个设置位)。
  • We use bitwise AND ( & ) instead of bitwise OR ( | ) to check if a bit is set. 我们使用按位AND( & )而不是按位OR( | )来检查是否设置了一个位。 This is because when we use bitwise AND with a number that only has a single bit set, the result will be a mask with the bit at the position of the 1 being in the same state as the corresponding bit in the original number (since anything AND 1 is itself), and all other bits being 0's (since anything AND 0 is 0). 这是因为当我们使用按位AND并且只有一个位设置的数字时,结果将是一个掩码,其中1位的位与原始数字中的相应位处于相同的状态(因为任何东西) AND 1本身),所有其他位为0(因为任何AND 0都是0)。
  • We want a mask with 1 in the position that we want to check and 0 elsewhere, so we need 1 << i instead of 0 << i . 我们想要一个在我们要检查的位置为1的掩码,在其他地方需要0,所以我们需要1 << i而不是0 << i
  • If the bit we're checking is set, we'll end up with a number that has one bit set, but that's not necessarily 1. So we should check if the result is not equal to 0 instead of checking if it's equal to 1. 如果我们正在检查的位被设置,我们最终会得到一个设置了一位的数字,但不一定是1.所以我们应该检查结果是否不等于0而不是检查它是否等于1 。
  • The == operator has a higher precedence compared to the | |相比, ==运算符具有更高的优先级 and the & operators, so parenthesis is needed. &运算符,所以需要括号。

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

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