DEV Community

Cover image for What the hell are Binary Numbers?
Harshit Chaturvedi
Harshit Chaturvedi

Posted on

What the hell are Binary Numbers?

Don't let binary numbers trouble you.

A quick intro

This is my first blog post ever! I am an aspiring backend developer who has never worked in Tech (been working in Customer Success) and is learning Python as his first programming language. Btw, I am learning from Boot.dev (Do check that out!).

Okay, coming back to intro. During my learning I came across Bitwise operators in python which are a type of comparison operators and I found myself a little confused about binary numbers, hence this post.

What are these numbers?

There are different number systems, base 2, base 8, base 10, base 16.

Computers use base 2 or binary numbers , 0 and 1 at its very core to store and process data. Computers use transistors for its main memory which either have on or off as their states. This on and off behaviour is represented by 0s and 1s.
More

For the sake of this blog, let's just take base 10 and base 2. Base 10 are your numbers 0,1,2,3,4,5,6,7,8,9 and base 2 are your binary numbers, 0 and 1.

In your everyday life, you are using base 10 as the number system, when you say any random number like 7, 281, 999, etc.

In base 10, there's a ones place, a tens place, hundreds place and so on. Similarly, base 2 also has places. They start at Ones place but every next place is multiple of 2. This is because binary is made of two bases, hence all of its digit positions correspond to powers of 2.

This is how the places would look like:

Binary looks like this

OR

We can also write these numbers as:

Binary Places with exponents

2 exponent 0 is 1.

Looking at these place in terms of exponents of 2 makes it easier for us to convert base 10 number to base 2 or decimal to binary which we will learn below.

Converting Binary to Decimal.

The conversion is pretty straightforward. Let's learn step-by-step.

Example 1: 1011

Steps

  1. Multiply each digit of the binary number with its corresponding power of two (from left to right):

    1 X (2^3) + 0 X (2^2) + 1 X (2^1) + 1 X (2^0)

  2. Solve the powers and add them:

    1 X 8 + 0 X 4 + 1 X 2 + 1 X 1 = 11

So, 11 is the decimal equivalent of the binary number 1011.

You, see how easy it was. Let's take another example:

Example 2: 10000000000

Steps

  1. Multiply each digit of the binary number with its corresponding power of two (from left to right):

    1 X (2^10) + 0 X (2^9) + 0 X (2^8) + 0 X (2^7) + 0 X (2^6) + 0 X (2^5) + 0 X (2^4) + 0 X (2^3) + 0 X (2^2) + 0 X (2^1) + 0 X (2^0)

  2. Solve the powers and add them:

    1 X 1024 + 0 X 512 + 0 X 256 + 0 X 128 + 0 X 64 + 0 X 32 + 0 X 16 + 0 X 8 + 0 X 4 + 0 X 2 + 0 X 1 = 1024

So, 1024 is the decimal equivalent of the binary number 10000000000.

Converting Decimal to Binary

Converting decimal to binary is pretty easy too. You keep on dividing the number by two until the quotient is you and the remainders at each step becomes the binary digit. And finally you read that number from bottom to top.

Example: 145

Decimal to binary

Now read the remainder column from bottom to top and that's your binary number : 10010001

Quick Quiz 😀

  • What do 1100100 and 1101 are equivalent to in decimal system ?

Open to feedback and questions. You can connect with me on Twitter (still not able to call it X)

Top comments (1)

Collapse
 
hellishbro profile image
HellishBro

awesome first post! a common mistake most beginning programmers do is assume that negative numbers in binary are just like the decimal system. in python it is like that and bitwise only do it on the unsigned part (the actual digits), but in other languages, the leftmost bit represents the sign instead, so bitwise operations in python wont match other languages