Here we go...
Even if you don't care much about binary numbers and this deeper knowledge from how the machine works, you surely even have asked yourself a simple question: "What the heck is going on here?!".
And at first sight all this processes under the hood can be a few scaring or a big hassle, but now I will show you that can be simpler than you ever saw turn integer numbers into binary โ and vice versa.
Step by step ๐ก
Define a number you desire
At this case, I will choose a random number, just for testing. So I will define 217
as my first number โ let's get started with a big number.
Get numbers from binary sequence
Each byte have 8 bits. Each bit is worth to a determined value into the queue, starting from right to left.
2โท | 2โถ | 2โต | 2โด | 2ยณ | 2ยฒ | 2ยน | 2โฐ |
---|---|---|---|---|---|---|---|
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
Separate the number in several sums results
Now it is the time to separate the number into sums that fill into binary sequence values. This process have to consider the sequence left-to-right from numbers, as the example below
128 โ 64 โ 32 โ 16 โ 8 โ 4 โ 2 โ 1
To do this, let's get 217
and separate into several operations, always separating the rest of the operations into new sum operations and considering the next number from sequence as target:
217 = 128 + 89
89 = 64 + 25
25 = 16 + 9
9 = 8 + 1
And it's done. Now we have the numbers from the binary queue (128 โ 64 โ 32 โ 16 โ 8 โ 4 โ 2 โ 1) that were within the initial integer number.
Put 1
at number that are in the sequence
Now, get all the numbers from the sequence, and set each space with 0
. And then, with numbers that you find out within the desired number, fill 0
's cells with 1
.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|
1 | 1 | 0 | 1 | 1 | 0 | 0 | 1 |
It's done ๐! Now we know that 217
, in binary format, is 11011001
.
Questions
But, if I had a binary number instead?
Then, you can do reverse engineering and just replace the spaces of sequence above with your binary number. And then, sum the numbers that have been filled with 1
.
What I could do with a number bigger than
256
?
Build the same method, but increase the exponent of 2
in the sequence. So, if the maximum at this example was 2^7
, you can add one more sequence to the left side, with the biggest value as
2^15
.
For example, 2^7 + 2^6 + 2^5 + 2^4 + 2^3 + 2^2 + 2^1 + 2^0
was the previous sequence, and the new with be 2^15 + 2^14 + 2^13 + 2^12 + 2^11 + 2^10 + 2^9 + 2^8 + 2^7 + 2^6 + 2^5 + 2^4 + 2^3 + 2^2 + 2^1 + 2^0
. And the binary number will be at this 00000000 00000000
(16 bits) instead of 00000000
(8 bits).
Conclusion
Computers' processes can be easier to understand than they seem. So, with this new quick tip, feel free to give your feedback and bring yet more value to discussion.
Made with ๐ by Cristian. Enjoy it!
Top comments (0)