bitwise negation problem

This is a discussion on bitwise negation problem within the C++ Programming forums, part of the General Programming Boards category; Let's make believe that we have a bool variable called giraffe. giraffe currently equals 0. Now let's say that we ...

  1. #1
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63

    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.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    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: ~.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Exclamation 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.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    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

  5. #5
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63
    Wow I never knew any of that stuff. Thanks a lot guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21