Need help understanding bitwise operators

I am having a hard time understanding what bitwise operators do and how I would use them. I have a very clear understanding of logical operators but I just can't understand bitwise operators. can you give me some examples please on how I would use them and what they are useful for and also can you please avoid using technical lingo as that only would confuse me more. thank you.
My own little contribution. You might have seen this used somewhere else before:

bool Even_B(int num)
    return ((num - 1) & 1));

The bitwise operation used in this is the bitwise and (&). It is called so because it compares 2 values and where ever the values have a matching 1, it sets a 1 at that index (so to speak). So in this case, if you pass the value 9 to the function it will compare the bits of 9 and 1 like so:

1001 => 9
0001 => 1

1000 => 8
0001 => 1
0000 => 0

return (0) (false)

value = 16

10000 => 16
00001 => 1

01111 => 15
00001 => 1
00001 => 1

return (1) (true)

Some people claim it makes for faster computations, this is debatable but if you know how to use them why not?
Last edited on
Probably a trivial example of faster computation, I often see a test for a number being odd or even like this:
if (number % 2 == 0) // number is even

Using bitwise operator
if (!(number & 1)) // number is even

Similarly, multiply/divide by a power of 2 can be done by bit-shifting, which is much faster than an actual division. Though I've heard it said that compiler optimisations can automatically substitute the faster code.

There are more serious uses too, such as storing data in a very compact form where space is limited, and data encryption or encoding etc.

A real-world example, a digital camera may produce data having 12 bits per pixel, this can be packed so that there are no wasted zero bits. In effect that means two pixels are made to fit into three bytes rather than four. Bitwise operations are used in the packing/unpacking process.
Last edited on
Im still confused, I understand what the operators mean but I still dont get wnat tnere doing.
They are manipulating values on the architecture level 0 and 1. Did you read the article by Disch and the link I posted? That was where I first learned about what bitwise operations are doing.
Ch1156: The article I linked had several practical examples. I don't think I can explain it any more than that unless you have specific questions.
I just dont understand where your getting the values from.
The 1's and 0's or the varaibles?
the ones and zeros I dont get it
They are called bitwise operations because they operate on the bits of a (integral) value on the left side of the operator. Generally for bitwise operations the left hand is the value and the right side is the mask, masking is a big reason they are useful but I'll get to that in a moment.
As I said the operations work at bit level, so for the value 6 the binary representation is
and if we AND (&) it with the value 2
each respective bit in 6 is AND'd with 2. What I mean is, bit 0 (for the sake of argument lets say the leftmost bit) is AND'd with bit 0 of 2 and the result is stored in bit 0 of (in this case) a temporary value. In my example of 6 & 2 the result is
or 2 so we know that bit is set because I used a mask that was a power of 2. That is where, in my opinion, AND and OR are most useful, you can use you one variable say a short to store 16 flags (true/false, on/off, etc...). If you use WinAPI you will see this a lot (in wparam and lparam I believe).

This stuff isn't the easiest thing to understand at first, until you get a solid understanding of how binary works. Try writing it out on paper, take 2 numbers convert them to binary (choose small numbers) and AND them , then OR them to get a feel for it. Windows calculator (if you use Windows) has a programmer mode that allows you to see how the bits are set for decimal values, you can use that to check your answers.
Last edited on
Ok I went and watched some tutorials on bitwise and they make more sense but now I'm having trouble figuring out when I would actually need to use them in a real world application especially in a game can you give me some examples?
Fast inverse square root comes to mind
Pokemon's shininess (generation III) is determined by using xor:
Ok so you can use bitwise operators to compact data? Like what could I compact? Text files or just code? How would I do this can you please give me another mini example
Remember the bitwise operators are just that, operators like multiply or add. They don't magically compact data.

One example where they have been used in the real world is to store boolean variables which require a yes/no value, using individual bits, rather than a whole byte each. Nowadays both disk space and RAM are so plentiful that it's hardly worth the trouble - except in special cases, perhaps some hardware device may have limited memory available to store settings etc.

If you want to compact general-purpose data, first you need an algorithm. Then you decide how to go about coding it, which may or may not require bitwise operations.
Last edited on
Ok I went and watched some tutorials on bitwise and they make more sense but now I'm having trouble figuring out when I would actually need to use them in a real world application especially in a game can you give me some examples?

Well... You can use one integer value instead of few boolean values to represent for example mob abilities (Walker, swimmer, flying):
#define WALKER 0x1
#define SWIMMER 0x2
#define FLYING 0x4
/* note values above;

int mob_ability=WALKER; // Defines mob to be only walker (0x1)
int mob_ability2=WALKER|SWIMMER; // defines that mob can move on ground and in water (0x1 OR 0x2 = 0x3->in binary 11 and in decimal 3)

bool mob_has_ability(int ability)
  return mob_ability&ability; // refer above and below

if (mob_has_ability(SWIMMER)) ..... // Seems mob has ability to swim
What you can do with these are very limitless (I believe) try finding different uses for them by trying to substitute normal operations like ' greater than' or 'less than' for bit wise operands. You will be amazed by what you can do once you start manipulating bits. Find primes using bitwise operations?? Possible? Hmm...
Topic archived. No new replies allowed.