0
help with bitwise operators
could someone explain exactly what bitwise operators do and how you're supposed to use them?
3 Respostas
+ 3
# comparing bits
A bit is the smallest possible piece of information, it can either be 1 (true) or 0 (false). Bitwise operators are operators (like +, -), operating on bits. Anything goes. For example:
AND (&) is only 1 if both bits you plug in are 1:
0 & 0 == 0
0 & 1 == 0
1 & 0 == 0
1 & 1 == 1
OR (|) is 1 if either bit is 1:
0 | 0 == 0
0 | 1 == 1
1 | 0 == 1
1 | 1 == 1
XOR (^) is 1 if only one input bit is 1:
0 ^ 0 == 0
0 ^ 1 == 1
1 ^ 0 == 1
1 ^ 1 == 0
NOT (!) reverses the input bit:
!1 == 0
!0 == 1
There are lots more - 20 in total, C++ only knows these four though I think.
# binary
Any number is stored as a series of bits in memory, also known as binary. You can transform a decimal number into binary by always removing the highest power of two, for example:
45 == 32 + 8 + 4 + 1 == 2^5 + 2^3 + 2^2 + 2^0, which we would write as 101101 in binary (no 2^4 or 2^1 in the solution so those bits are 0).
# bitwise operators
A bitwise operator now operates on these bits of a number (or two numbers). For example:
45 & 30, would be
101101 & 011110 == 001100
which is 12 in decimal. Every bit of the left number is compared with the bit in the right number at the same position.
# bit shifts
There's also bit shifts, which also count as bitwise operators in C++. They shift the binary representation to the left or right. Example:
001100 >> 1 == 000110
001100 << 2 == 110000
# when to use them
Domen gave probably the biggest use case - you can store multiple options inside a singe int, but he got it a bit wrong. The idea is to set a single bit in each option, and you can then check using & (not |!) whether that bit is set.
Of course, that's a bit oldschool, you probably don't need to be doing that a lot. Bitwise operators are also important when doing low-level things (say, writing a driver for a graphics card), and also some game/AI stuff (check out bitboards). It's nice to know about them, sometimes they pop up in really unexpected locations.
0
Bitwise operators are a comparison between to values, and it can either be a yes or no for the answer. So:
if 1 == 2 {
cout << "Hallo!";
//This won't happen! 1 does not equal 2.
}
if 7 >= 4 {
cout << "Foo";
// This will happen, 7 is greater than
// (or equal to) 4
}
0
Bitwise operators as the name implies manipulate bits. You usually don't need that aspecially beginners should avoid them if possible.
Numbers are structured as binary bits so for e.g. int number 5 would be 00000101
You can manipulate those bits woth bitwise operators (and, or, xor, logical shifts) so for e.g. you could use bit masking to enable stuff:
// disclaimer: don't use global vars (here only to shorten example)
int sound = 1;
int video = 2;
int subtitles = 4; // must be next bit - 3 would just set the first two variables
void play(int options)
{
if(options & sound) play_sound();
if(options & video) play_video();
if(options & subtitles) show_subtitles();
}
// call somewhere
play(sound | video);
Function will only play soudn and video but wont show subtitles as (001 | 010)== 011 (if bit on one side is set then it s set in result) and (011 & 001)==001 (if bit is set on both sides then it is set in result) so for subtitles the result would be 0 which is equal to false as (011 & 100)==000.
But as said before don't use bitwise operations if possible as this code would be much nicer if function would accept three bool variables.
Edit: I've wrote the correct explanation of the example but shomhow swapped & with | after the internet connection failed and had to rewrte the answer in a hurry... Thx @Schindlabua for spotting that