Setting bits

This is a discussion on Setting bits within the C++ Programming forums, part of the General Programming Boards category; I was wondering what the easiest way to set the bit of an int in position x to 0? A ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    48

    Setting bits

    I was wondering what the easiest way to set the bit of an int in position x to 0? A push in the right direction would be great (it'd be nice if there was a standard function, but I don't know if that exists)

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Use the bitwise operators and operations (shifting/negating/etc...):
    Code:
    value &= ~(1<<x);
    Last edited by hk_mp5kpdw; 06-03-2005 at 12:47 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    That clears a bit at a specified location.

    To set a bit at a specified location you want

    Code:
    value |= (1 << x);
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >That clears a bit at a specified location.
    I was wondering what the easiest way to set the bit of an int in position x to 0?

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I see. Well I guess it was a little poorly worded or misinterpreted by mean. I took the OP's meaning to mean, what is the easiest way to set the bits in an int from position x to 0 as in positions 3, 2, 1, 0. Like a range set. Oh well.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Quote Originally Posted by MrWizard
    Oh well.
    ++m_pMrWizard->reputation;// heheh (made me chuckle)

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I see. Well I guess it was a little poorly worded or misinterpreted by mean.
    I must confess I misinterpreted it the first time too. But seeing it was hk_mp5kpdw who posted the first reply, and as he's a resident guru, I took a second look.

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Buckshot,
    "Set to zero" is confusing because:

    Set = make a bit one (or true)
    Clear = make a bit zero (or false)

    There is some really good bit manipulation information in the Programming FAQ.

    One of the tricks is to use hex instead of decimal. Decimal is almost never used with bit manipulation. In the hardware world we use a mix of binary and hex - "Bit 7 is supposed to be low, but I'm reading an E."

    In C++ programs, we usually just use hex, because binary I/O is a pain (not built-in to iostream). To C++, an integer is just a number (stored in binary). It doesn't "care" if the input/output format is hex, decimal, or binary.

    It is easy to convert between hex and binary. You can learn to to the conversions in your head. Once you learn 16 conversions (0 thru F), you can convert big numbers. And, you really only have to learn 14... You already know 0 and 1 !

    FYI - The Windows calcualtor can do hex to binary conversions (in the scientific mode).

    Here's an example program I wrote a couple of years ago:
    Attached Files Attached Files
    Last edited by DougDbug; 06-03-2005 at 06:54 PM.

  9. #9
    Registered User
    Join Date
    May 2005
    Posts
    48
    Sorry about the confusion, in the original post I was wondering what the best way to clear position x would be.
    A question about the first reply:
    If you try to clear a position that is already zero with that code you will end up setting the bit to 1.
    For example:
    you have the number: 1011 0110
    and want to clear the 4th place from the right (it is a zero if the places are numbered 8765 4321, the value of x however would be 3)
    you would end up with a bitwise and for this:
    1011 0110
    &1111 0111
    =1011 1110


    Also, another question, is there anyway to check whether a specific bit in a number is set or not?
    Thanks

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >If you try to clear a position that is already zero with that code
    >you will end up setting the bit to 1.
    Think about it. You're taking a single set bit and shifting it n places. To clear or set the 4th bit, you would end up with this as the mask:
    Code:
    0000 1000
    By complementing the mask for clearing a bit, you have this:
    Code:
    1111 0111
    Let's see what happens if you want to clear a cleared bit:
    Code:
    1111 0111
    0000 0000
    &
    0000 0000
    Since both bits are cleared, and & returns true of both bits are set, the result is still a cleared bit. So there's no change

    If you want to set a bit that's already set, do the same thing:
    Code:
    0000 1000
    0000 1000
    |
    0000 1000
    Since one or more of the bits are set, the result is a set bit. Once again, no change.

    >is there anyway to check whether a specific bit in a number is set or not?
    Code:
    #include <iostream>
    
    namespace jsw {
      inline bool is_set(unsigned x, int i)
      {
        return (x & (1U << i)) != 0;
      }
    
      inline void set(unsigned& x, int i)
      {
        x |= (1U << i);
      }
    
      inline void clear(unsigned& x, int i)
      {
        x &= ~(1U << i);
      }
    }
    
    int main()
    {
      unsigned x = 0;
    
      std::cout<< std::boolalpha << jsw::is_set(x, 3) <<'\n';
      jsw::set(x, 3);
      std::cout<< std::boolalpha << jsw::is_set(x, 3) <<'\n';
      jsw::set(x, 3);
      std::cout<< std::boolalpha << jsw::is_set(x, 3) <<'\n';
      jsw::clear(x, 3);
      std::cout<< std::boolalpha << jsw::is_set(x, 3) <<'\n';
      jsw::clear(x, 3);
      std::cout<< std::boolalpha << jsw::is_set(x, 3) <<'\n';
    }
    My best code is written with the delete key.

  11. #11
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Quote Originally Posted by Prelude
    >If you try to clear a position that is already zero with that code
    >you will end up setting the bit to 1.
    Think about it. You're taking a single set bit and shifting it n places. To clear or set the 4th bit, you would end up with this as the mask:
    Code:
    (1U << i);

    you assuming the architecture... what happnes when this code is run on a mac.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >you assuming the architecture... what happnes when this code is run on a mac?
    Uh, let me take a guess, the same thing that happens when you run this code any other platform?

  13. #13
    Registered User
    Join Date
    May 2005
    Posts
    48
    Grr....dumb mistake. Had to recheck how the and operator worked.... wow I feel dumb.
    Thanks for the help!

  14. #14
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    ok lets assume the machine is big-endian
    Code:
    byte to check            :-    00000010
    mask 1(big endian)    :-    10000000
    
    now i want to check if the 1st bit is set which in the above case is yes
     1U << 1      :-    00000000
    now    00000010
    &        00000000
    -----------------------
              00000000
    
    and the result is 0 which is the bit is not set... but the bit is actually set...
    Now I am confused.. may be I am wrong can some one clarify...

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    C++ does not give a damn about endianness as long as you don't try to split a larger datatype into smaller pieces. In other words, the shift and all other bitwise operators work exactly the same on all platforms, as long as you only use unsigned types. (Signed types are a different issue.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. setting the values of bits
    By yahn in forum C++ Programming
    Replies: 14
    Last Post: 04-14-2008, 01:09 AM
  2. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 04:03 AM
  3. Help counting number of bits set in an integer
    By JayDiddums10 in forum C Programming
    Replies: 5
    Last Post: 12-07-2006, 03:21 PM
  4. Setting bits
    By Kinasz in forum C Programming
    Replies: 9
    Last Post: 07-05-2004, 06:12 AM
  5. Writing binary data to a file (bits).
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 04:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21