Results 1 to 11 of 11

Thread: bit comparing in c++

  1. #1

    Thread Starter
    Hyperactive Member Cmdr0Sunburn's Avatar
    Join Date
    May 2001
    Location
    g0t r00t?
    Posts
    461

    bit comparing in c++

    ok
    i need to check if bit 5 is set (left to right), all the other bits can be set or not but as long as bit 5 is, how would i go about doing this.

    00000000 00000000 00000000 00010000
    I know a lot oF Vb, expert in C++, and i think in assembly.
    MSVC++6.NET
    vb6
    masm
    Windowz Xp
    I find my self using this a lot in C++

    __asm {
    }

  2. #2
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Code:
    //0x01==0001b,0x02==0010b,0x04==0100b,0x08==1000b,0x10==10000
    if(0x10 & my32bitvariable)
    {
    //it is set, so do what you want here
    
    }

  3. #3
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    More readably,
    Code:
    if(my32bitvariable & (1 << 4))
    (It's really the 4th bit, since the counting is zero-based.)

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    There is no such thing as the 0th something.
    It's the 5th bit, the one with index 4.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    I call the rightmost bit the 0th bit. *shrug*

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Sure, and there are 1024 metres in a kilometre
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    shh

  8. #8
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    CB - Take a look in my sig!
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Where do you think I have it from McCain?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    I just read it and thought "cool, that's in my sig!" I never thought that someone as good as you are would "learn" anything from someone like me, I feel honored
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594


    I think it's cool.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width