Thread: Checking bits in a bitwise operation

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    32

    Checking bits in a bitwise operation

    I'm working on a homework assignment, and this specific part of it is confusing me:

    Then use masking and shifting operations to decode the number. For each of the four possible diagnostics, mask off the right-hand bit. If it is 1, set the corresponding level variable to true. Then right-shift one bit and repeat.

    My only problem is that I don't know how to refer to this specific bit and check it. I tried doing this:

    Code:
    if (arg[0] == 0b1)
              boolVar = true;
    arg = arg >> 1;
    //repeat for other bits
    and learned that apparently you can't treat a number in this case like an array. So how would I be able to refer to this bit?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    Ė David J. Wheeler

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    10
    Code:
    int a = 3;
    
    //test if Low Order bit is true
    if( a & 1)cout << "Least significant bit is 1\n";
    what happens here is the value stored in a 0000 0011 is bitwise-anded with 0000 0001 which returns 0000 0001. which is true!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Bitwise Operation..
    By ahmedBanihammad in forum C Programming
    Replies: 4
    Last Post: 10-31-2010, 09:11 AM
  2. Bitwise operation; flag look up
    By emptythought in forum C Programming
    Replies: 22
    Last Post: 09-08-2010, 06:48 AM
  3. int into bits Bitwise
    By bobdillion in forum C++ Programming
    Replies: 74
    Last Post: 05-21-2010, 02:17 AM
  4. Bitwise operation problem
    By cunnus88 in forum C++ Programming
    Replies: 9
    Last Post: 10-25-2008, 01:49 PM
  5. Bitwise operation
    By Justice in forum C Programming
    Replies: 4
    Last Post: 02-09-2008, 09:06 PM