Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
C++
    unsigned char y = 0x0;
y = y | 1; //Set Bit 0
y = y | 8; //Set Bit 3
y = y &~1; //Clear Bit 0
y = y &~8; //Clear Bit 3

I was reading on bitwise op's, and got confused by one thing, `y = y | 1` sets the Oth bit to 1, how do you set the 1st bit to one? How do you do the last? Also how do you decide which number sets which bit?

Thanks.
Posted

Simple.
C++
y = y | 2;   //set Bit 1
y = y | 128; // set Bit 7

Consider the binary representation of the first few digits.
0 = 0000 0000
1 = 0000 0001 *
2 = 0000 0010 *
3 = 0000 0011
4 = 0000 0100 *
5 = 0000 0101
6 = 0000 0110
7 = 0000 0111
8 = 0000 1000 *

Notice the ones marked with a *, they're the ones that have only 1 bit set. These are the numbers you use to set/clear a particular bit. Notice also, that the *ed items are all powers of 2? You have probably guessed by now that you can fiddle with the other bits of an 8 bit variable by using the numbers 16, 32, 64, 128.

Hexidecimal numbers are often easier to work with than decimal ones when dealing with bit-fields. This is because each nibble or hexadecimal digit is equivalent to 4 bits.
This would leave you with the following numbers to set/clear bits in an 8 bit number:
(bit 0)0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80(bit 7)
It's much easier to see at a lance that each of the above numbers have one bit set and which one it is, than it would be with their decimal equivalents.
 
Share this answer
 
v2
Comments
Leo Chapiro 20-Jul-15 10:07am    
Perfect explained, +5 !
enhzflep 20-Jul-15 10:49am    
Thank-you. :)
If I could explain things 1/2 as well as Richard Feynman could, I'd die a very content man.
Are you serious? It's 2 == 1 << 1.
2N, where N = 0, 1, 2… presents a bitset where N-the bit is set and all other bits are clear. Those numbers are
1 << 0 == 1,
1 << 1 == 2,
1 << 2 == 4,
...
1 << N...
I don't even understand what needs explanation here. This is one of the fundamentals of mathematics, computers science and practical programming you need to understand perfectly, otherwise you cannot do anything at all in programming.

See also:
https://en.wikipedia.org/wiki/Binary_number[^],
https://en.wikipedia.org/wiki/Binary_code[^].

—SA
 
Share this answer
 
v5
Comments
0xF4 18-Jul-15 14:23pm    
I SAID `I was reading on bitwise op's` Here is a link to Google Translate http://transalate.google.com please translate it to a language you understand. What I know is that you read on a thing because either you have forgotten it or you don't know it.
[no name] 18-Jul-15 15:06pm    
"I don't even understand what needs explanation here"....


Please give People the Chance to learn here...and do your best to explain and not "only" criticize....

I know usually you educate, please educate also Basics. Thanks.

Maybe "educate" in the above needs to be replaced by "learn"....
Frankie-C 18-Jul-15 16:53pm    
5'd
Simple functions to set or reset the Nth bit are based on this concept.

unsigned SetNthBit(unsigned val, unsigned cBitN)
{
return val | (1 << cBitN);
}


unsigned ResetNthBit(unsigned val, unsigned cBitN)
{
return val & ~(1 << cBitN);
}
[no name] 18-Jul-15 20:50pm    
Countered
Sergey Alexandrovich Kryukov 18-Jul-15 21:48pm    
Thank you, Wes.
—SA
Everything is in bitwise operation meaning.

In the cpu, integers are stored in a binary form. it means that each bit hold the value of a power of 2.
bit 0 has the value of 2^0, bit 1 has the value of 2^1 and so on.
10 (base 10) is 1010 (base 2)
which is 1*2^3+ 0*2^2+ 1*2^1+ 0*2^0

Bitwise means that the operation is applied to each bit without link between bits (no carry).
Bit n of the result is the result of the operation applied to bit n of the operands.

One advantage of bitwise operations is parallel treatment, each operation is applied to each bit of the operands in parallel.
 
Share this answer
 
v3
Comments
Patrice T 19-Jul-15 5:42am    
Thanks for counter vote.
It is an easy thing...after get familar with it, but this needs its time.

In short:
0 && 0 == 0
0 && 1 == 0
1 && 0 == 0
1 && 1 == 1

Longer Version, but Overkill to start:
https://en.wikipedia.org/wiki/Boolean_algebra[^]

Simpler Version:
https://simple.wikipedia.org/wiki/Boolean_algebra[^]

Use this to find more understanable explanations:
https://www.google.ch/#q=boolean+algebra+simple+explanation[^]

Hope it helps.
 
Share this answer
 
v2
Comments
Stefan_Lang 20-Jul-15 10:55am    
You are using the logical operators not the bitwise operators in your example code. They won't work as expected with the example code posted in the question!
[no name] 20-Jul-15 11:12am    
You are right, but it shows the logic...! And if you think there is a difference betwe(except the Operator Symbol) than I think you are on a wrong way ;)
Stefan_Lang 21-Jul-15 2:13am    
The author specifically asked about bitwise operators, not boolean logic. There is a considerable difference between the two:
6|3 == 7
6||3 == true (1 in older versions of C/C++)
6&3 == 2
6&&3 == true (1 in older versions of C/C++)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900