Thread: Testing ~bit

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    54

    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');
    		con << "Bad Input! Incorrect Answer.\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');
    	}
    Last edited by JeremyCAFE; 01-05-2006 at 08:26 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (userInput == ~byte1 & 255)
    Well you need to go look at your operator precedence table.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    54
    sorry, that was actually a bad copy and paste. I have if (userInput == (~byte1 & 255))

    what i am confused on is the & 255

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    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. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    54
    Quote 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. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    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
    Last edited by DougDbug; 01-05-2006 at 04:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 64 bit testing
    By DrSnuggles in forum C++ Programming
    Replies: 7
    Last Post: 11-20-2007, 03:20 AM
  2. C++ bit testing
    By Vicious in forum C++ Programming
    Replies: 3
    Last Post: 09-19-2004, 11:44 AM
  3. Blending and Depth Testing
    By Thunderco in forum Game Programming
    Replies: 2
    Last Post: 03-08-2004, 06:37 PM
  4. about testing a program
    By Abdi in forum C Programming
    Replies: 1
    Last Post: 06-09-2002, 12:51 AM
  5. Testing Testing 123.. Oh yeah and...
    By minime6696 in forum Windows Programming
    Replies: 0
    Last Post: 08-13-2001, 09:07 AM