Binary Conv
Binary Conv
Binary Conv
Binary Numbers
A Binary Number is made up of only 0s and 1s.
So each digit has only two possibilities: 0 or 1
Bits
In the computer world "binary digit" is often shortened to the word "bit"
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
00
01
10
11
000
001
010
011
100
101
110
111
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
1
1
0
1
0
1
0
1
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
0 1 2 3
0
10
11
9
1001
10 11 12 13 14
15
1010
1111
1011
1100
1101
1110
This is quite a useful thing to remember. If you forget how the sequence
of binary numbers go, just think:
"0" and "1" {0,1}
then "0" and "1" again but with a "1" in front: {0,1,10,11}
then take those four with "1"s as a third digit:
{0,1,10,11,100,101,110,111}
and so on!
Or just count exactly like decimal numbers, but don't use 2,3,4,5,6,7,8
or 9.
Formula
21
22
23
24
25
26
etc...
Settings
2
4
8
16
32
64
etc...
Example: when you have 50 binary digits (or even 50 things that can
only have two positions each), how many different ways is that?
Answer 250 = 2 2 2 2 ... (fifty of these) = 1,125,899,906,842,624
So, a binary number with 50 digits could have 1,125,899,906,842,624
different values.
Or to put it another way, it could show a number up to
1,125,899,906,842,623 (note: this is one less than the total number of
values, because one of the values is 0).
Hexadecimal
Lastly, I would like to tell you about the special relationship between
Binary and Hexadecimal.
There are 16 Hexadecimal digits, and we already know that 4 binary
digits have 16 possible values. Well, this is exactly how they relate to
each other:
Binary:
10
11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111
Hexa- 0
decimal
:
So, when people use computers (which prefer binary numbers), it is a lot
easier to use the single hexadecimal digit rather than 4 binary digits.
For example, the binary number "100110110100" is "9B4" in
hexadecimal. I know which I would prefer to write!
Decimal vs Binary
Here are some equivalent values:
Decima 0
l:
Binary:
10
11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110
10 11
12 13
14
15
1111
20
25
30
40
50
100
200
500
Binary:
10100
11001
11110
101000
110010
1100100
11001000
111110100
10.1
Example: 10.1
The "10" means 2 in decimal,
The ".1" means half,
So "10.1" in binary is 2.5 in decimal
1234 = ((1*10^3)+(2*10^2)+(3*10^1)+(4*10^0))
Lets start with what a binary number looks like:
01001010
2 to the power of 0 = 1 (2^0)
2 to the power of 1 = 2 (2^1)
2 to the power of 2 = 4 (2^2) or (2*2)
2 to the power of 3 = 8 (2^3) or (2*2*2)
2 to the power of 4 = 16 (2^4) or (etc.)
01001010 = 64 + 8 + 2
which means that 01001010 = 74
Successive subtraction:
Now lets do the inverse. To convert 74 to binary, youll have to start by finding
the biggest power of 2 that is valued less then 74. Finding this number is
important because it will determine the positive value at the left of your binary
number. In this case, it is 64.
Lets put a 1 in the 2^6 (2*2*2*2*2*2 = 64) position.
01000000
After, take your initial decimal number, and subtract it by the value you just
found out. 74 64 = 10. Now, lets do the same procedure as before. What is
the biggest power of 2 that is valued less then 10? It is 8.
Put a 1 in the 2^3 position ( 2*2*2 = 8 )
and continue doing this until the total equals your initial decimal number.
01001010
Even numbers always finish with a 0 and odd ones end in 1. This is because
the rightmost digit in a binary number can only have a decimal value of 0 or 1.
Successive Devision:
Another way to convert from decimal to binary is by successive divisions.
74 / 2 = 37
37 / 2 = 18
18 / 2 = 9
9 /2= 4
4 /2= 2
2 /2= 1
Remainder
0
1
0
1
0
0
1 /2= 0
(74)10 = (1001010)2
(1001010)2 = 0x20+1x21+0x22+1x23+0x24+0x25+1x26
= 2 + 8 + 64 = (74)10
Binary addition
works in the same way as the decimal system, except that only 0's and 1's can
be used, instead of the whole spectrum of 0-9. This actually makes binary
addition much simpler than decimal addition, as we only need to remember the
following:
0+0=0
0+1=1
1+0=1
1 + 1 = 10
As an example of binary addition we have,
101
+101
a) To add these two numbers, we first consider the "ones" column and
calculate 1 + 1, which (in binary) results in 10. We "carry" the 1 to the "2s"
column, and the leave the 0 in the "ones" column.
b) Moving on to the "2s" column, we calculate 1 + (0 + 0), which gives 1.
Nothing "carries" to the "4s" column, and we leave the 1 in the "2s" column.
c) Moving on to the "4s" column, we calculate 1 + 1, which gives 10. We
"carry" the 1 to the "8s" column, leaving the 0 in the "4s" column.
101
+101
1010
Another example of binary addition:
1011
+1011
10110
Note that in the "2s" column, we have 1 + (1 + 1), where the first 1 is
"carried" from the "ones" column. Recall that in binary,
1 + 1 + 1 = 10 + 1
= 11
upposeyou'reeditingatextfilewithatexteditor.Becauseyou'reusinga
texteditor,you'reprettymucheditinganASCIIfile.Inthisbrandnewfile,
youtypein"cat".Thatis,theletters'c',then'a',then't'.Then,yousavethe
fileandquit.
Whathappens?Forthetimebeing,wewon'tworryaboutthemechanism
ofwhatitmeanstoopenafile,modifyit,andcloseit.Instead,we're
concernedwiththeASCIIencoding.
IfyoulookupanASCIItable,youwilldiscovertheASCIIcodefor0x63,
0x61,0x74(the0xmerelyindicatesthevaluesareinhexadecimal,instead
ofdecimal/base10).
Here'showitlooks:
ASCII
c'
a'
t'
Hex
63
61
74
Binary 01100011
01100001
01111000
EachtimeyoutypeinanASCIIcharacterandsaveit,anentirebyteis
writtenwhichcorrespondstothatcharacter.Thisincludespunctuations,
spaces,andsoforth.Irecallonetimeastudenthasused100asterisksin
hiscomments,andtheseasterisksappearedeverywhere.Eachasteriskused
uponebyteonthefile.Wesavedthousandsofbytesfromhisfilesby
removingcomments,mostlytheasterisks,whichmadethefilelooknice,
butdidn'taddtotheclarity.
Thus,whenyoutypea'c',it'sbeingsavedas01100011toafile.
When arranged in sets of 8 bits (1 byte) 256 values can be represented (0-255).
Using an ASCII chart, these values can be mapped to characters and text can be
stored.