The bit world as readable as possible

Tobias Aguiar
5 min readNov 23, 2022

--

Photo by Markus Spiske on Unsplash

What is a bit?

We can define a bit as the numerical representation that can only takes to possible values : 0 or 1. Or as you might prefer, on or off. It is the numerical system used by your computer, and many other devices.

Your computer can do things like this:

Photo by Szabo Viktor on Unsplash

And this :

Photo by Ilya Pavlov on Unsplash

But it only understands this :

binary numbers, picture from this link

Have you ever wodered why that is? To answer this question, you need to understand that your PC — or almost any device — has this embedded with it :

A general cpu from this link

Which are made of a buch of these :

A simple transistor

And, to make it short, this little electronic device just understands two things : on and off.

So that’s why we use so frequently the binary numerical (and also the hexadecimal) system when working as embedded software engineer. This introduction was to say that knowing how to manipulate bits when programming in a embedded software project turns out to be really important.

Bitwise operations

Almost every operation involving binary numbers can be structured as follows:

Operand <operation> operand = result

The only exception is the NOT operation, which can take only one operand.

But why could such operations be useful? Because you’re going to deal with different kinds of processors, and when you do low-level programming, you enable or disable specific functionalities and communication by manipulating registers bits! (I’ll write a little bit about all of this soon).

Quick reminder : bitwise operations are not the same thing as logical operations, they are used in different contexts! To be short : logical operation are used to verify if something about the two numbers are true or false (used quite often with the “or” and the “and” operators). Whereas the bitwise operation are used only to modify each bit of a number! I’ll show some examples.

Also, when you are doing logical operation, you use the symbol twice! Execpt for the NOT operation.

Here are some of the operations we can do with :

OR ( | ) operator

An “OR” operator (symbol | used in programming languages) is an operation where taking two operands whose values can only be true or false, returns true if one of the operands takes the true value, as shows the truth table below:

Now let’s take an example with a bitwise “or” and the logical “or”. Let’s suppose I have two byte (8 bits) numbers (unsigned char, for example) and I’ll perform a bitwise operation with them :

6 (0000 0110 in binary) | 1 (0000 0001)

If we take each bit of the two numbers and we operate as the truth table says, we’ll obtain : 7 (0000 0111).

Whereas if we do a logical “or”, as we were in an “if” statement in programming, if the number is above zero, it will be considered true! So in the end you are going to be doing :

6 (6>0 so true) | 1 (1>0 so true) = 1 | 1 = 1!

This operation is really useful, for example, to set a bit value to 1.

AND ( & ) operator

An “AND” operator (symbol & used in programming languages) is an operation where taking two operands whose values can only be true or false, returns true only if both of operands have value true, as shows the truth table below:

If we take the same example as the “or” example, we’ll have :

6 (0000 0110 in binary) & 1 (0000 0001)

If we take each bit of the two numbers and we operate as it says the truth table, we’ll obtain : 0 (0000 0000), because there is no equal bit if we compare the two numbers each bit.

But, if we do a logical operation, as if we were in an “if” statement :

6 (6>0 so true) & 1 (1>0 so true) = 1 & 1 = 1!

This operation is really useful, for example, to set a bit value to 0.

NOT (~) operator

A “NOT” operator (symbol ~ used in programming languages) is an operation where taking just one operand whose values can only be true or false, returns its opposite value, as shows the truth table below:

Taking the two numbers as an example :

6 (0000 0110) => ~6 = 249 (1111 1001)

1 (0000 0001) => ~1 = 254 (1111 1110)

XOR () operator

A “XOR” operator (symbol ^ used in programming languages) is an operation where taking two operands whose values can only be true or false, returns true if they shows opposite values between each other, as shows the truth table below:

Taking the same example as the previous ones, we’ll have :

6 (0000 0110 in binary) ^ 1 (0000 0001) = 7 (0000 0111)

This operation is very useful when you want to toggle a specific bit.

More resources

You can find more resources about this topic in the following links :

. Neso academy : Bitwise operations in C (Youtube) (Part 1)

. Neso academy : Bitwise operations in C (Youtube) (Part 2)

. Neso academy : Bitwise operations in C (Youtube) (Part 3)

. Neso academy : Bitwise operations in C (Youtube) (Part 4)

Quick remind that this is only an introduction to the field, and also there is more operations than those I presented here. Hope you find the lecture useful!

Happy reading!

--

--

Tobias Aguiar

Software developer | Trying to make complex concepts look easy | Want help or discuss about embedded software development? Email me! tobi.aguiar01@gmail.com