DEV Community

loading...
Cover image for Let's talk about bits

Let's talk about bits

thisismanaswini profile image Manaswini Originally published at manaswini.hashnode.dev ・5 min read

A bit is a unit used in computations. It is short for Binary Digit. It can take two values, either 0 or 1. All of what is done by computers has to do with manipulating these bits. All the arithmetic, execution of instructions, data transfer and storage etc... narrows down to either a 0 or a 1.

Today, in this post we'll try to understand what bits are, some terms related to them and conversion of a decimal number to a binary number and vice-versa.

Some terms

First let's familiarize ourselves with a few terms that you will find when you start to consume literature related to bits.

1) Byte : A byte is basically a collection of 8 bits. For example, 01100000 is a byte, 10100100 is also a byte. A byte can be used to represent any number between 0 and 255, both included. I'll explain more about that later in the post.

2) Word : A series of bits grouped to represent a number.

3) Binary number : A number expressed using bits.

4) ^ : This is a notation that I'll be using in this article to mean "to the power of". So for example, 3^2 means 3*3 = 9

How to represent a number using bits ?

In the definitions for a byte and a binary number, I spoke about how bits can be used to represent any number ( between 0 and 255, both included ). I'll explain how this is done now.

Binary number to a decimal number

A binary number is expressed in the base 2 numeral system. When we work normally with everyday numbers, we use the decimal system, which is a base 10 numeral system.

That is to say, if we have a number let's say, 183, we can express it in decimal system as 100 + 80 + 3 = (1*100) + (8*10) + (3*1). If we had 7706, we could express it as (7*1000) + (7*100) + (0*10) + (6*1). Notice the numbers in bold. Let's call them multipliers.

For 183, the multipliers were 100, 10 and 1

For 7706 the multipliers were 1000, 100, 10 and 1.

You might have observed the following for these multipliers.

Any given multiplier is ten times the multiplier next to it in the decimal system. In other words, when we go from the right most multiplier to the left most one, we iterate through the powers of 10.

This is what we mean by a base 10 numeral system, which is our everyday decimal system. The binary number system, like I said before, is a base 2 numeral system.

This should mean that every multiplier to be used in the binary system should be two times the next multiplier. In other words, when we go from the right most multiplier to the left most one, we iterate through the powers of 2.

So if I choose a random number in the binary system, say, 0110111, then to express it in our everyday number system, we need to multiply each bit in it by a multiplier and then add the result of all these multiplications to get that final decimal number.

The multipliers are 64, 32, 16, 8, 4, 2 and 1. (64 is two times 32, 32 is two times 16 and so on...)

Following equation shown pictorially

=> 0110111 = (0*64) + (1*32) + (1*16) + (0*8) + (1*4) + (1*2) + (1*1) = 0+32+16+0+4+2+1 = 55

i.e. 0110111 in binary system is equivalent to 55 in the decimal system.

Decimal number to a binary number

Now if we want to go from decimal to binary, we need to work our way backwards. Hold on! Don't worry. I'll give you an example that will make this clear.

Let's choose 74.

Here are the powers of 2 up till 2^7

Powers of 2 : 2^0=1, 2^1=2, 2^2=4, 2^3=8, 2^4=16, 2^5=32, 2^6=64, 2^7=128

The power of 2 that is immediately smaller than 74 is 64. We need to write 74 in terms of addition of powers of 2 starting from 64 and going down till 1.

74 = 64 + 8 + 2 = (1*64) + (0*32) + (0*16) + (1*8) + (0*4) + (1*2) + (0*1)

This looks similar to the form we saw in the example of converting a binary to a decimal number. Let's write both the forms together and try to correlate them

First example :

0110111 = (0*64) + (1*32) + (1*16) + (0*8) + (1*4) + (1*2) + (1*1) = 55

Above equations shown pictorially

Second example (present) :

74 = (1*64) + (0*32) + (0*16) + (1*8) + (0*4) + (1*2) + (0*1) = ?

Above equations shown pictorially

Don't they look almost alike? By comparison, we get that for the second example the binary form should be 1001010.

Therefore 74 in the decimal system is equivalent to 1001010 in the binary form.

So we learnt how to convert a decimal number to a binary number and vice-versa. What next?

Remember, in the definition of a byte, I told you that a byte can be used to represent any number between 0 and 255. All the explanation till now was to prove that.

A byte, if you recall is basically a combination of 8 bits. It can be any combination of 0s and 1s as far as the total number of bits in it are 8.

So the smallest combination (in terms of value) is eight 0s placed back to back i.e. 00000000 and the largest number is eight 1s placed back to back i.e. 11111111. If we apply the techniques we learnt before and try to convert 00000000 and 11111111 to the decimal system, guess what we'll get.

000000000 in binary is equivalent to 0 in decimal system.

11111111 in binary is equivalent to 255 in decimal system.

Numbers from 0 to 255 can be represented using a byte

That means we can represent any number between 0 and 255, both included using 8 bits i.e. using one byte.

Today we learnt
1) What a bit, byte, word and a binary number are

2) We learnt about how we can convert a binary number into a decimal number and vice-versa

3) At last we proved how we can represent any number between 0 and 255 (both included) using one byte ( i.e. using 8 bits )

Next Steps
In the next article, I'll talk about how we can perform simple logical operations using bits and learn about logical gates.

Discussion (6)

pic
Editor guide
Collapse
ljcdev profile image
ljc-dev

Great article 😁! I wanted to learn more on bits. Looking forward to your next one πŸ‘Œ.

Collapse
thisismanaswini profile image
Manaswini Author

Glad it was helpful to you JC😊

Collapse
beabkal profile image
Beabkal

"Let's talk a bit about bits" would have made an interesting title :).
Nice article!

Collapse
thisismanaswini profile image
Manaswini Author

Yeah! That would've been great :D

Collapse
mateuszjarzyna profile image
Mateusz Jarzyna

Notice the numbers in bold - formatting failed, no bold numbers

Collapse
thisismanaswini profile image
Manaswini Author

Sorry! I'll correct that right away