# bitwise negation problem

Printable View

• 03-15-2004
Mr_Jack
bitwise negation problem
Let's make believe that we have a bool variable called giraffe. giraffe currently equals 0. Now let's say that we use the bitwise negation operator on giraffe like so:
giraffe = ~giraffe;
giraffe should equal 1 now, yes? Well aparently it doesn't, why is this? Your help would be more appreciated than you could possibly comprehend.
• 03-15-2004
jlou
The bool type is not meant to hold bits or numbers of any kind. In fact, bool never equals 0, it equals true or false.

If you want to negate a bool value, use !giraffe instead.

Some integer based types can be used with boolean checks. For example, windows code uses BOOL, which is just a typedef for an unsigned int. If you do a bitwise negation, then instead of 1 you will get 4294967295. That is because the bits go from 00000000000000000000000000000000 to 11111111111111111111111111111111.

If you are using something as a boolean (true or false) use the logical not operator: !. Only if you are using something to hold bits, should you use the bitwise not operator: ~.
• 03-15-2004
DougDbug
Bitwise operators affect ALL of a number's bits!
If you haven't done so already, read the section in the Programming FAQ that covers bitwise operations. When you perform a bitwise operation, ALL OF THE BITS in the number are affected.

In C++, a bool type is somewhat abstract. Like jlou said, the "value" of a bool is supposed to be "true or "false"... not a numerical value! In fact, true is probably stored as a binary one, and false as binary zero. The C++ language standard states: "An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true becoming one."

The computer hardware cannot read a single bit. If you are running a 32-bit machine, each physical memory address holds a 32-bit value. If you want to know the value of a particular bit at a particular address, you have to read the whole 32-bit word, and then use the bitwise operators to "mask-out" the bits you're not interested in. This is (usually) the whole point to bitwise operators... to manipulate individual bits that can't be individually accessed.

Bitwise operators are mostly used for low-level (hardware) programming, where one bit represents one "thing" or one condition, like the state of an LED or a switch.

However, programming students are usually introduced to bitwise operators as an encryption technique.
• 03-15-2004
Dave Evans
Try this C++ program with Borland bcc or GNU g++:
Code:

#include <iostream>

using namespace std;

int main()
{
bool x;
bool y;
x = false;
y = !x;
cout << "x = " << x << ",  y = " << y << endl;
printf ("x = %08x,  y = %08x\n\n", x, y);
y = ~x;
cout << "x = " << x << ",  y = " << y << endl;
printf ("x = %08x,  y = %08x\n", x, y);

return 0;
}

Then do the same thing in C, with <stdio.h> instead of <iostream>, int instead of bool for x and y (and without the cout <<, of course).

(Microsoft VC++ did give a warning with the C++ program about "unsafe use of type bool" with the ~ operator: not recommended, but gave the same result as the others.)

You really aren't supposed to use ~ with type bool, but sometimes programmers get away with things they aren't supposed to do --- often causing grief for themselves or others somewhere down the line.

Dave
• 03-15-2004
Mr_Jack
Wow I never knew any of that stuff. Thanks a lot guys