# Testing ~bit

This is a discussion on Testing ~bit within the C++ Programming forums, part of the General Programming Boards category; I am a little confused on how to test user input with the correct answer of a complementary bit. A ...

1. ## Testing ~bit

I am a little confused on how to test user input with the correct answer of a complementary bit. A labby told us just to AND it with 255, but I do not understand why we are doing that.

byte1 is an randomly generated unsigned char from 0 - 255
userInput is an intiger

Code:
```// Asks for answer of ~byte1
con << "What is the COMPLEMENT result of " << int(byte1) << " ("; toBin(byte1); con << ")?\n";
con << "> ";
if(!(cin >> userInput))
{
cin.clear();
cin.ignore(20, '\n');
}
else
{
// Checks to see if the answer is correct
if (userInput == (~byte1 & 255))
{
numCorrect++;
con << "Correct!\n";
}
else
con << "False!\n";

cin.ignore(20, '\n');
}```

2. > if (userInput == ~byte1 & 255)
Well you need to go look at your operator precedence table.

3. sorry, that was actually a bad copy and paste. I have if (userInput == (~byte1 & 255))

what i am confused on is the & 255

4. What does the complement of a number mean?
What does a bitwise AND do?
What does the unary ~ do?
Answer those 3 questions (should be easy to find answers online!) and you'll know why you're doing that.

5. Originally Posted by Stuka
What does the complement of a number mean?
What does a bitwise AND do?
What does the unary ~ do?
Answer those 3 questions (should be easy to find answers online!) and you'll know why you're doing that.
unsigned char byte = 32;
// Binary version: 00100000
// ~byte bin: 11011111 (223)
// 255 Binary: 11111111

// ~byte & 255: 11011111 (223)

I know this has something to do with the fact that ~byte is a char (or that was the impression I got off the poor explenation)

Right now I see ~byte being the same as ~byte & 255
Just trying to figure out what i am missing as far as understanding

Does this have to do something with converting ~byte to an int to be compared?

6. Right now I see ~byte being the same as ~byte & 255
Just trying to figure out what i am missing as far as understanding... Does this have to do something with converting ~byte to an int to be compared?
It's a bit-mask. As long as you're working with type char, you might be OK. But on some systems, a char may hold more than 8 bits. A bitwise-and with FF hex (255) masks-off the upper bits (forces any upper bits to zero) without changing bits 0 thru 7.

Example:

With 8 bits ~3 = 252 decimal = 1111 1100 binary = FC hex

With 16 bits ~3 = 65,532 decimal = 1111 1111 1111 1100 binary = FFFC hex

With 32 bits ~3 = 4,294,967,292 = 1111 1111 1111 1111 1111 1111 1111 1100 binary = FFFFFFFC hex

BIG HINT -
When working with binary numbers or bitwise operations, programmers almost always use hexadecimal in the program code, and for input/output. (The exception might be with "easy" numbers like 0, 1, 255, etc.)

Also, when bitwise operations are used, the "number" usually conceptually represents a bit-pattern, rather than a numeric value... We generally don't care what the number is, but we want to know the state of a particular bit.

You can learn to convert between hex and binary in your head (any number of bits/digits!) if you memorize the following table:
0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
7 = 0111
8 = 1000
9 = 1001
A = 1010
B = 1011
C = 1100
D = 1101
E = 1110
F = 1111
Code:
` cout << hex <<  X << endl;   // Display X in hexadecimal`
Even if hex is not requred for your current assignment, it will help with debugging and testing your program.

And if you don't know already, the standard Windows calculator can convert between decimal, hex, binary, and octal: START-> PROGRAMS-> ACCESSORIES-> CALCULATOR-> VIEW-> SCIENTIFIC