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)
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)
Use the bitwise operators and operations (shifting/negating/etc...):
Code:value &= ~(1<<x);
Last edited by hk_mp5kpdw; 06-03-2005 at 11:47 AM.
"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
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
>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?
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
++m_pMrWizard->reputation;// heheh (made me chuckle)Originally Posted by MrWizard
>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.
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:
Last edited by DougDbug; 06-03-2005 at 05:54 PM.
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
>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:
By complementing the mask for clearing a bit, you have this:Code:0000 1000
Let's see what happens if you want to clear a cleared bit:Code:1111 0111
Since both bits are cleared, and & returns true of both bits are set, the result is still a cleared bit. So there's no changeCode:1111 0111 0000 0000 & 0000 0000
If you want to set a bit that's already set, do the same thing:
Since one or more of the bits are set, the result is a set bit. Once again, no change.Code:0000 1000 0000 1000 | 0000 1000
>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.
Originally Posted by Prelude
you assuming the architecture... what happnes when this code is run on a mac.
>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?
Grr....dumb mistake. Had to recheck how the and operator worked.... wow I feel dumb.
Thanks for the help!
ok lets assume the machine is big-endian
Now I am confused.. may be I am wrong can some one clarify...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...
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