# How to Train Your Python: Part 18, Introduction to Bitwise Operators

Welcome back! In our last training session we covered classes and how OOP can make programming/scripting easier. Today we'll be taking a breather and covering something fairly simple, bitwise operators.

Bitwise operators are relatively simple. Essentially, when we use bitwise operators, we're writing in binary. Ones and zeros. We use bitwise operators to represent integers in binary. Today we're just going to learn how to read bitwise operators.

We'll start by going over some key terminology, then we'll cover octets and their bit place values, the base two number system, and finally we'll read some binary. So, let's get started!

## Basic Terminology

If we're going to read binary, we'll need to know the terminology. Let's define some key terms that we'll be using frequently:

• Octet: A group of eight bits (one byte)
• Bit: One eighth of a byte, exists as a one or zero
• Base two number system: The system used to determine a bit places value

If you have any questions about this terminology, please leave them in the comments section or just message me. You must understand these terms in order to proceed.

Now that we know some basic terminology, let's explain the octet a bit more.

## Explaining the Octet

Now, we've already discussed that one octet consists of eights bits. Also, eight bits makes up one byte. Now, each bit in an octet hold a different value.

These place numbers are numbered 0 through 7 (Just like indexing). The thing is, they're numbered in reverse. When we read/write binary, we have read from right to left. Let's see a representation of an octet:

Here we can see our eight bit places with their respective numbers.

Now that we know about the layout of an octet, we can discuss the base two number system.

## The Base Two Number System

Now that we've covered the layout of an octet and it's bit place values, we can discuss the base two number system. This is the system used to determine the decimal value of a bit based on it's position in the octet.

Bits can either be one or zero. A one indicates that bit as being on, while a zero indicates it as being off. If a bit is off, we don't calculate it's value. We only calculate the values of bits that are on.

As the name implies, the base two number system works with a base number of two. In order to find the decimal value of a given bit, we take the number two to the power of that bits position.

Let's see the base two number system in action. Let's look at the previous bit numbering diagram, but this time we'll add the decimal value:

There we go! We can see that by taking two to the power of the bits positional value, we can calculate the decimal value of the bit!

## Calculating the Whole Octet

Now that we know how to evaluate individual bits, we can calculate the value of the whole octet!

We're going to look at our diagram again, but this time we're going to switch the bit position numbers with ones and zeros. We'll then calculate the value of these positions, and use these values to find the value of the whole octet! Let's start by seeing the ones and zeros:

Ok, so we see here that we have a mix of ones and zeros. Our goal is to calculate the total value of the octet, so we must exclude any bit positions that are zero (off). Let's go ahead and mark these positions with a red X to exclude them from our calculation:

Alright, now that we know which bits we need to calculate for, let's do that. Remember, we need to take two to the power of the bits position. You can refer to the diagram under the base two number system section to review this.

Let's go ahead and calculate the values of our bits, we'll put the decimal value underneath them:

There, now that we have our bit values calculated, we just need to total them up to find the value of the octet:

We've successfully calculated our octets value! The binary numbers 01100101 are equal to 101. Let's wrap this up, shall we?

## Wrapping It Up

Today we covered octets/bits, the base two number system, and how to convert binary to decimal (integers). We didn't do any coding this round, this is so we can understand what's happening before we do it. We won't be going in blind now! Next time we'll cover how to actually use bitwise operators in Python.

-Defalt

Want to start making money as a white hat hacker? Jump-start your white-hat hacking career with our 2020 Premium Ethical Hacking Certification Training Bundle from the new Null Byte Shop and get over 60 hours of training from ethical hacking professionals.

• Hot
• Latest