Bitwise Operators in C (with exercise)

Just a guy who loves to write code and watch anime.
Search for a command to run...

Just a guy who loves to write code and watch anime.
No comments yet. Be the first to comment.
Qualities that make outstanding builders.

Bipedal vs quadrupedal: how many legs This is about the number of legs the creature walks on. Bipedal. Two legs. Humans, ostriches, T-rex, kangaroos, most fantasy humanoids. Quadrupedal. Four legs. Do

Intro You hear "lerp" and "smoothstep" everywhere in game dev. They sound like math jargon. They're not. Both are small tools that do the same job: smoothly move from one value to another. The problem

What is Kinematics Kinematics is the math field. It's the study of how things move without worrying about forces (which would be dynamics). FK and IK are the two branches: forward kinematics and inver

Intro Textures are usually the biggest cost in a 3D scene. Memory, bandwidth, and load time all get eaten by them. Resizing them is the obvious lever. There's more. This post is about the less obvious

In this post, I'll explain how bitwise operators work in C.
We'll start with binary code, bitwise operators, and at the end, I'll give you a beginner exercise.
Computers work with binaries. It's the programming language they understand based on a 2-number system, 0s and 1s. They represent the off-and-on states of transistors inside a computer's microprocessor.
We can perform operations on the bits of the binary using bitwise operators.
Let's take a look at different numbers in binary values:
int num1 = 5; // Binary: 0101
int num2 = 10; // Binary: 1010
int num3 = 17; // Binary: 10001
int num4 = 255; // Binary: 11111111
int num5 = 1023; // Binary: 1111111111
Binary code to decimal numbers isn't hard. You take the power of two, starting at 0 to the right, and increment it by one as you move to the left.
To break it down, if we've the binary number 1110 :
Rightmost bit: 0 (the bit) x 2 ^ 0 = 0
Second rightmost bit: 1 (the bit) x 2 ^ 1 = 2
Third rightmost bit: 1 (the bit) x 2 ^ 2 = 2 x 2 = 4
Fourth rightmost bit: 1 (the bit) x 2 ^ 3 = 2 x 2 x 2 = 8
The entire equation: 0 + 2 + 4 + 8 = 14.
So 1110 is equal to 14.
This part can be confusing. But before we dive into each operator, one main thing you've to understand:
The position of the numbers in the binary matter. So when compared or calculated, it's as if you're stacking them on top of each other, going column by column.

There are 6 bitwise operators.
Each bit of the output is 1 if the corresponding bits of both inputs are 1. Otherwise, it's 0.
int a = 12; // Binary: 1100
int b = 10; // Binary: 1010
int result = a & b; // Result in Binary: 1000 (Decimal: 8)
Each bit of the output is 0 if the corresponding bits of both inputs are 0. Otherwise, it's 1.
int e = 12; // Binary: 1100
int f = 10; // Binary: 1010
int result = e | f; // Result in Binary: 1110 (Decimal: 14)
Each bit of the output is 1 if the corresponding bits of the inputs are different. Otherwise, it's 0.
int j = 12; // Binary: 1100
int k = 10; // Binary: 1010
int result = j ^ k; // Result in Binary: 0110 (Decimal: 6)
int o = 12; // Binary: 1100
int result = ~o; // Result in Binary: 0011 (Decimal: 3)
Shifts the bits of the number to the left and fills the empty bits with zeros.
Below we're shifting each bit two times to the left.
int r = 3; // Binary: 0011
int result = r << 2; // Result: 1100 (Decimal: 12)
Shifts the bits of the number to the right.
Below we're shifting two times to the right.
int u = 16; // Binary: 10000
int result = u >> 2; // Result in Binary: 00100 (Decimal: 4)
Use bitwise operators to determine whether the function is odd or even.
Revising how to convert binaries to decimal numbers helps here. Think about the possible values and what scenario has to be true for it to be odd.
int is_even(int number) {
// TODO: Return 1 if number is even,
// 0 otherwise using bitwise operator.
// C doesn't have booleans, hence we use 1 to determine `true`
}