This is the beginning of a series on Binary Numbers and Bitwise Operators which will culminate in solving the 5 recommended Binary Algorithm Challenges from the LeetCode Blind 75.
What are Binary Numbers?
Binary is a method which computers use to represent numbers and data. You have probably heard of "zeros and ones". These are called 'bits' of data and they exist on a much lower level (i.e. closer to the hardware) than the higher-level programming languages the average developer works with such as Python or JavaScript.
Binary numbers can be measured in different units:
Bit: A bit is the smallest unit in binary, it represents one single value, either 0 or 1.
Nibble: A nibble is 4 bits!
Byte: A byte is 8 bits. From this we can infer the meaning of other units such as mega, kilo, giga, tera, and petabytes...
Binary numbers exist ultimately on a hardware level, specifically electronic circuitry! I am no electrical engineer, but a simple understanding of what happens on a computer chip has helped me to understand what binary is all about.
The Transistor
The transistor is considered one of the most important inventions of the 20th century. Transistors used to look something like this:
but now they look more like this:
and billions of them can exist on a single computer chip. An electric current will flow through them, and each has only two states, on or off, zero or one. This may remind you of boolean values in high-level programming languages, and you would be right to make this connection! On/Off, True/False, and 0/1 are all valid ways to think about transistors and binary in general.
When we write code we are essentially manipulating the computer chip at a hardware level, and building electronic circuits with transistors which will carry out our logical tasks. But I digress...
Base 10
Ok, so let's talk about numbers. The numbers we use every day are generally in what is called Base 10. In Base 10, each column of the number represents a certain multiple of ten. For instance, the right most digit always represents a number of 1s, up to the number 10. The 2nd column from the right represents 10's, so if you had a 3 in this column for instance, it would mean that there are 3 10's in this number. Here's an example:
1000 | 100 | 10 | 1 |
1 5 3 2 = 1,532
Base 2
Binary numbers are represented in Base 2. In Base 2, each column of the number represents a multiple of 2, remembering that the only values we have to work with are 0 and 1. So each column in base 2 can only be a zero or a one. That looks something like this:
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
0 0 1 1 0 1 0 1
= 00110101 in binary = 53 in base 10
Just add up the values at each column to determine what the number is in binary! This binary number has 1 in the 32 column, 1 in the 16 column, 1 in the 4 column, and 1 in the 1 column, which makes this number 32 + 16 + 4 + 1 = 53.
Adding numbers in Base 10
We should be familiar with adding two numbers together in Base 10. Lets add 63 and 58.
63
+58
---
121
When the two numbers in one column add up to 10 or more, a one is carried over to the next column to the left. We add up each column from right to left until we have added both numbers together.
Adding numbers in Base 2
In Base 2 or Binary, we add numbers in a similar way. The only difference is if the value of the two digits added equals 2 or more, than we carry a 1 over to the next column. Let's see this in action:
00110 = 6
+11011 = 27
------
100001 = 33
Try this yourself and you will see that any time you add 1 + 1 in a column, the result in that column will be 0, and a 1 will carry over one column to the left. In this case, there will be a carry in every column until we finally add a 16's column at the end of the addition in the left-most position.
Conclusion
Adding two positive numbers together in binary is fairly simple, but what if we want to subtract two numbers? This is not as straight forward in binary and requires a trick called Two's Complement which we will look at in the next post...
Top comments (0)