---
title: 'Bitwise Operators and WHY we use them'
source: 'https://youtube.com/watch?v=igIjGxF2J-w'
video_id: 'igIjGxF2J-w'
date: 2026-08-02
duration_sec: 520
---

# Bitwise Operators and WHY we use them

> Source: [Bitwise Operators and WHY we use them](https://youtube.com/watch?v=igIjGxF2J-w)

## Summary

This video explains bitwise operators, which are often misunderstood by programmers. It covers why they are useful, especially for memory-constrained devices, and demonstrates each operator (AND, OR, XOR, NOT, left shift, right shift) with examples using binary numbers and file permissions.

### Key Points

- **Introduction to Bitwise Operators** [00:00] — Bitwise operators work on individual bits and are essential for algorithms like encryption and video compression. They were more common when memory was limited but remain useful today.
- **Why Use Bitwise Operators?** [00:57] — The smallest addressable unit is a byte, but booleans only need one bit. Storing multiple flags in a single byte saves memory, especially on small devices.
- **File Permissions Example** [01:50] — Unix file permissions like 644 or 777 use three numbers representing permissions for owner, group, and others. Each number is a binary representation of read (4), write (2), and execute (1) permissions.
- **AND Operator** [02:59] — The AND operator (single ampersand) returns 1 only if both bits are 1. For 7 (111) and 6 (110), the result is 110 (6). It can be used to extract specific permissions.
- **OR Operator** [04:11] — The OR operator (single pipe) returns 1 if at least one bit is 1. For 7 and 6, the result is 111 (7). It can combine permissions, e.g., read (4) OR write (2) gives 6.
- **XOR Operator** [05:23] — The XOR operator (caret) returns 1 if bits are different. For 5 (101) and 6 (110), the result is 011 (3). It can toggle permissions, e.g., removing a permission.
- **NOT Operator** [06:01] — The NOT operator (tilde) flips all bits. For 5 (101), the result is 010 (2) in a simple 3-bit example, but in Python's 8-bit representation, it becomes -6 due to two's complement.
- **Left and Right Shift** [07:20] — Left shift (<<) moves bits left, effectively multiplying by powers of two. Right shift (>>) moves bits right, dividing by powers of two. For example, 5 << 2 = 20, and 20 >> 2 = 5.

### Conclusion

Bitwise operators are powerful tools for low-level programming, enabling efficient memory usage and direct manipulation of bits. Understanding them is crucial for fields like encryption and hardware control.

## Transcript

So today we're going to be talking about bitwise operators. that aren't really understood by a great deal of programmers. So you can kind of get away without understanding them.
and you never know when you might need to use them. If you end up writing algorithms for encryption or video compression, In my last video, I talked about binary numbers.
As you can probably guess from the name, bitwise operators work on individual bits. and perform operations on them, then you need to use bitwise operators.
So before we get started, let's have a look at why we would want to use them. used to be used a lot more when computers didn't have as much memory as they do now. limitation, then they're still very, very useful and you need to know them.
The reason for this is it's the smallest unit of addressable space that that can only really have the values, true and false, should only really take up one bit.
with the rest of the seven bits just padded out. if you've got a small device, thats got a limited amount of memory. Now you've probably used flags in your application, such as boolean true
Now most applications, especially those that are referencing physical hardware, will have multiple flags that you want to use. in your application and each of them are taking up a byte,
Now, one of the things that programmers do to save space is to store multiple Boolean flags using one byte. As a byte is eight bits, Which is just what you need
The one place that you might see So you might have seen file permissions with things like 644 or 777.
make up the different groups, for the permissions on the Unix filesystem. the second number will be the group they belong to, Each number tells you the permissions that the particular group
So, they'll tell you whether you can read the file, write the file, or execute it. You need to convert the number into binary first. would be like 7, which in binary is 111.
Then there's 6, which in binary is 110. Which means And lastly, there's 4, which in binary is 100,
but they can't write to it or execute it. Now that we have an example in mind, let's have a look at the bitwise operators and we're going to have a look at each of them and work out what they do.
This is normally denoted with a single ampersand in most programing languages. The best way to describe these operators is by using truth tables. So if we have a look at the numbers, seven and six,
the seven is 111 and six is 110. it does a comparison between each of the bits in the number. each of those bits match and if they do, then it will give us a one.
Here we can see that the first two rows have 1 in each column. for those rows. For the last row, the bits don't match, So our final value for doing 7
and 6 is going to be 110 and therefore the result is 6. Going back to our file permission example, the operator can actually be used as the operator only gives us back the matching bits.
then we're going to get back our required permissions as the final result The next operator we are going to look at is the OR operator.
and will give you a result if either one or the other is set to one. we're going to see that for every row, that at least one of them is one.
And therefore, the final result is going to be 111, which is 7 in decimal. multiple permissions, so say you have the read permission and the write permission and
You can use the OR operator in this case to combine those permissions set. If we do that in a code example, this is what it would look like. with the OR, and then the final permission is therefore going to be 6.
Let's say we want to give the user the execute In this case, we can use the OR assignment operator
So we have read, write permissions If we add execute permissions on top, that's going to give them a final result Now this requires each of the bits to be the opposite of each other.
If we look at the values five and six again. For the first row, is going to be zero with an exclusive OR. But for the second two rows, the columns are either zero or one and
we have the value 011 as the result, which would be three in decimal. If we go back to our permissions example, we can use the exclusive OR
So let's say permissions from the user. You can do this with an exclusive OR operator on assignment.
The NOT operator is going to give you the opposite value for each of the bits the NOT operator doesn't work on two values, The NOT operator will take a number, put it into bits
If we take the number 5, for example. Five in binary is 101. is going to be 010.
to get a negative number as the result, which might be a bit surprising at first. the first bit of that number denotes the sign.
If it's a zero, it's going to be a positive number. So the first bit is going to become one As Python uses an 8 bit number to represent small numbers.
Five is going to look like this in binary if we use the NOT operator all of the bits get reversed and therefore it becomes this. Binary numbers use something called the twos complement representation.
it starts with one, the rest of the number is counting up from -128. So if we take this number and start from -128 and then add on
all of the other bits, we're going to get the final result of -6. are the operators left shift and right shift. As the name suggests, these operators shift the bits of a number either left or right.
on a single number and the second number denotes So if we take the number five in binary, which is this. we're going to be moving all of those bits two bits to the left.
When you're shifting numbers to the left, the leftmost bits of the number In our case, that is zero anyway, so it doesn't actually make a difference. you're going to be losing some of those bits when you move it up.
to be 20. If we then take 20 and then shift it two bits to the right, it's no surprise we getthe  number five again. And that's really all
It can seem a bit scary at first, If binary numbers are a bit confusing, then make sure you watch my last video Hope you found this video useful.
Thank you for watching and I'll see you next time.
