Binary and Hexadecimal Basics

binary
hexadecimal

#1

Hello once again 0x00’ers,

Today I will write up a short guide on binary and hexadecimal.

Binary is a number system based on powers of 2. The only “numbers” are 1 and 0(on and off for electrical currents).
We normally use decimal on a day to day basis which is powers of 10 (10, 100, 1000,…)

Binary

Binary goes from right to left and starts at zero. I know something that helped me learn/memorize this with programming so i’m going to share it with you. You normally use ordinal numbers because you are talking about order(First, Second, Third, etc) Think of ORDinal like ORDer. You can’t have Second without First, Third without Second, and so on. Now think of cardinal numbers. It starts at Zero not One(or First :stuck_out_tongue:) With cardinal numbers you can pick out numbers like a card(CARDinal, CARD). This applies more to lists in programming but I still think it’s good to think of it that way.

Example

128, 64, 32, 16, 8, 4, 2, 1
0 0 0 0 0 0 0 0
2^7||2^6||2^5||2^4|| 2^3||2^2||2^1||2^0

if you wanted to write 1 in binary It would be 0001. You would not need to write the other zeros to the left like
0000 0001.

To convert it to decimal you would multiply the number(1 or 0) by 2 to the power of(^) the place it’s in. Remember to go right to left. I wrote the decimal numbers above, 0 in binary in the middle, and the places at the bottom.
so starting from the rightmost place we have a 1. 1 * 2^0 is one(remember, when a number is raised to the power of zero it's always one.) Since we have only zeros left we are done!

Lets do some more so you get the hang of it. Lets do 12 in binary.
1 1 0 0
(notice you don’t have to write the full thing out for numbers less than 15/1111 in binary)

Now to convert 1100 to decimal we use the same trick. We start at the rightmost number and it’s a zero. So we go one place left. Still a zero. Another place left and it’s a 1. We do 1 * 2^2 and get 4. then we go left a place and there is a 1 so we do the same thing. 1 * 2^3. we get 8. 8 plus 4 is 12 So we did it.

One more this time lets go past 15. 0001 0001
1 * 2^0 + 1 * 2^4 is 17. so 0001 0001 is 17 in binary.

##Hexadecimal

Hex is similar to binary but it is based off of powers of 16, so instead of x * 2^y it’s x * 16^y.
Hex is very easy to convert to binary and vice versa. all you need to do is memorize binary up to 15(for basic hex numbers).

Hexadecimal starts at 0(shocker!) and ends at 15 (thus powers of 16) however once you get to 9 hex goes by letters until you get to F(15). I will add tables at the very end for you guys. Hexadecimal is commonly prefixed with 0x.

Examples and how to convert to decimal
0x05
We start from the right and there is a 5 so we do 5 * 16^0 and that is 5. so 0x05 is 5 in decimal

0xb3
Starting from the right there is a 3.
3 * 16^0 is 3 but wait! there’s more ( ͡° ͜ʖ ͡°). There is a b(but Clust3r, that’s not a number!!!). Remember when I said once you get to 9 it switches to letters. Well there you go. So b is 11 (…, 9, A, B, …). So we do 11 * 16^1 and get 176. Add the two together and you get 179. So 0xb3 is 179(don’t worry about the ‘0x’).

Converting Binary to hex is simple. Just find each binary value and write it. If binary number is greater than 15(ex: 1010 1010) then you split the byte into 4 bits and treat them seperate.

0010 1100
0010 is 2(1 * 2^1)
1100 is 12(1 * 2^2) + (1 * 2^3)
so it’s 0x2c

So hopefully you know the basics of hexadecimal, binary, and how to convert them to decimal. Here is that chart I promised(I will only write it to 15/ 0000 1111 because numbers 15+ follow the same patterns). If you like this I might make a more intermediate guide on datatypes and such… and as always…stay frosty :snowman2:

BINARY =, then HEX

0000 = 0 || 0x00
0001 = 1 || 0x01
0010 = 2 || 0x02
0011 = 3 || 0x03
0100 = 4 || 0x04
0101 = 5 || 0x05
0110 = 6 || 0x06
0111 = 7 || 0x07
1000 = 8 || 0x08
1001 = 9 || 0x09
1010 = 10 || 0x0a
1011 = 11 || 0x0b
1100 = 12 || 0x0c
1101 = 13 || 0x0d
1110 = 14 || 0x0e
1111 = 15 || 0x0f


Counting in Any Number System
#2

I’m honestly amazed I didn’t notice this! Great tutorial! Well written and cleared a lot up as well :smile:


#3

Thank you, I’m glad it helped. :wink:


(Arm adillo) #4

Thanks for this. Always was curious about Hex but not a rabbit ho;e that I’ve ever gone now. Now I know. And knowledge is power.