Thread: Finding out if a bit is set using logic operators...

  1. #1
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Post Finding out if a bit is set using logic operators...

    This might sound like a really newbie question, but truth is I havn't worked with this stuff in a longgg time. My question is how to find out if a bit is set in a word (16 bit or otherwise) using logic operators.

    I remember a while back doing something like enumerating the bits by powers of two,

    bit1 1
    bit2 2
    bit3 4
    bit4 8

    etc. and then using the NAND operator to extract it... I just don't remember how.

    Thanks ~SPH

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    27
    number & (1UL<<n)

    if you want to see if the 6:th bit is set in number ,then n = 6
    The result of the operation is 1 if its set and 0 if its not.

  3. #3
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    just one thing

    One problem, what is 1UL ?

    Hex?

    Thanks, ~SPH

  4. #4
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    nevermind

    Nevermind I figured it out, 1UL is just 1 heh', thanks .

    ~SPH

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    27
    UL = unsigned long

    i think :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ bitwise operators with 64 bit integers? possible?
    By chrisd1100 in forum C++ Programming
    Replies: 8
    Last Post: 09-04-2007, 07:13 PM
  2. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  3. how to set bit X of Y to 1?
    By evader in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2001, 03:30 PM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM