1. 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. Use the bitwise operators and operations (shifting/negating/etc...):
Code:
`value &= ~(1<<x);`

3. That clears a bit at a specified location.

To set a bit at a specified location you want

Code:
`value |= (1 << x);`

4. >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. 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.

6. Originally Posted by MrWizard
Oh well.

7. >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. 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:

9. Sorry about the confusion, in the original post I was wondering what the best way to clear position x would be.
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. >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';
}```

11. 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. >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. Grr....dumb mistake. Had to recheck how the and operator worked.... wow I feel dumb.
Thanks for the help!

14. ok lets assume the machine is big-endian
Code:
```byte to check            :-    00000010

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. 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.)