Thread: Not quite clear

  1. #1
    KingoftheWorld
    Guest

    Not quite clear

    Hi All,

    I am not quite clear understanding what the C macro below do:
    Code:
    #define BIT(word, i) (((word) & 1 << (i)) >> (i))
    
    /* Pass data into BIT macro */
    
    BIT(0x00FF, 5);
    Thanx for any detail explanation for above code.

    KW

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Given a value 'word', and a number 'i', do the following:

    Shift the value of 1 'i' places to the left.
    Use a bitwise AND to see if that bit is set on the value 'word'.

    Take the result of that & operation, and shift it back 'i' places.

    Thus:

    0001000100010001

    Let's say we check the 5th place.

    10000

    0001000100010001 & 10000

    Thus, we have a "true", or a 1.

    10000 >> 5 times is 1

    In a nutshell, it just tests to see if a single bit is set, and it returns a "true" or "false" (a 1 or a 0). There really is no point in going through the additionall right shifts. Just test your value for zero, and if it's not zero, the value is set.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    KingoftheWorld
    Guest
    Thanks! quzah.

    KingogtheWorld
    ==================
    Think globally, Act locally.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About clear(). Please help!
    By Antigloss in forum C++ Programming
    Replies: 12
    Last Post: 07-14-2005, 04:02 AM
  2. How to Clear in Visual C++
    By MyDestiny in forum Windows Programming
    Replies: 4
    Last Post: 03-16-2005, 10:40 PM
  3. How to Clear in Visual C++ 6.0
    By MyDestiny in forum Windows Programming
    Replies: 1
    Last Post: 03-16-2005, 11:57 AM
  4. Yet another clear screen thread :D
    By kermit in forum Linux Programming
    Replies: 2
    Last Post: 11-20-2003, 05:14 AM
  5. Using a button to clear a list box!
    By Unregistered in forum C++ Programming
    Replies: 13
    Last Post: 08-23-2002, 07:44 PM