Thread: Bitwise Operations to Read ON/OFF Bits

  1. #1
    Registered User
    Join Date
    Feb 2008

    Question Bitwise Operations to Read ON/OFF Bits

    I am new to C, and I'm stack on code for bitwise operation

    I need to get a function (which i have a call from main) using 3 integers to say if bits in a range are all on or off


    Number , is the integer
    X is the beginning position of the range
    Y is the ending position of the range

    so far the only solution that i thought of was:

    (((Number >> X) & 01) & ((Number >> Y) & 01))

    but it doesnt seem to work.
    Anyone able to help me?

  2. #2
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    one of the solutions - write function that checks that left n bits are all ON using bitmask tables

    unsigned char check[] = { 0x01, 0x03, 0x07, ...};
    int checkLeftBits(unsigned char value, size_t numberOfBits)
       unsigned char cleardValue = value & check[numberOfBits-1]; /* clear bits that are out of interest */
       return (cleardValue  == check[numberOfBits-1]);
    of course this sample lacks checks for suitable range of the second parameter which should be from 1 to CHAR_BITS

    the second step will be to take the original value, shift it on X bits left and based on Y-X value - decide what bytes of the number should be passed into the function above with what number of bits too check)
    To be or not to be == true

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Lets have an example:
    Number is 0011001101011100
    X = 3
    Y = 11
    I'm starting at zero as the rightmost binary digit. You didn't give the conventions so I'll make my own. The mask we want is:

    How can we make this mask? Well...
    1<<3 == 0000000000001000
    1<<11 == 0000100000000000

    Now subtract one from those:

    Can you now see how we can get the mask using 'xor', and then solve the problem using an 'and'?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007

  5. #5
    Registered User
    Join Date
    Feb 2008
    Thanks all for the replies, I managed to figure it out with iMalc's help, and it works. Thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise Operators
    By rrc55 in forum C Programming
    Replies: 6
    Last Post: 04-30-2009, 11:37 AM
  2. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  3. bitwise negation problem
    By Mr_Jack in forum C++ Programming
    Replies: 4
    Last Post: 03-15-2004, 07:16 PM
  4. read bit's from any file
    By scrapedbr in forum C Programming
    Replies: 1
    Last Post: 09-30-2003, 03:12 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM