Reading And Programming In Binary
Other
|
Binary is the basis of all systems. The only language that computers know is binary, a two-base numerical system; consisting of only 1s and 0s. For example, here's the number 105 in binary:
|
CODE:
|
Copy / Restore :: Remove Scroll Bars
|
Select what you want to copy and in doing so you will keep the formatting when pasting it.
|
|
Wait, what?
I'm sure you were thinking that, so let me explain.
A great way to interprete binary is using the "bowl" method. Say you have an infinite line of bowls. Starting with the first one on the left. The first bowl has a value of 1, and each bowl to the left has a value of double of the previous bowl. Next, you have some stones. You can only hold a single stone in each bowl. Your total value is added by the stones in the bowls. Here's an example:
[512] [256] [128] [64] [32] [16] [8] [4] [2] [1]
The bowls in BOLD contain stones.
You'll see that the first bowl that contains a stone is bowl "64". Your total number is 64. The next bowl to contain a stone is bowl "32". Your total is now 86. Next is bowl "8" (104), and finally bowl "1" (105).
Simple, right?
But what about letters?
Very good question!
You would use ASCII (American Standard Code for Information Interchange) values for that. For example, the ASCII value for the letter "A" is 65. So you would use the number 65 in binary, like so:
|
CODE:
|
Copy / Restore :: Remove Scroll Bars
|
Select what you want to copy and in doing so you will keep the formatting when pasting it.
|
|
Easy enough to grasp.
You can find a list of every ASCII value and character here:
http://www.techonthenet.com/ascii/chart.php
Wow... That was easy!
Sure was! But now, how would you program with binary?
There are 6 main types of calculations (called bitwise logical operations) in programming for binary, and most languages support them.
The six types of bitwise operators are:
-Bitshift left: Represented by a pair of left angle brackets <<
-Bitshift right: Represented by a pair of right angle brackets >>
-Bitwise AND: Represented by a single ampersand &
-Bitwise OR: Represented by a single vertical line |
-Bitwise XOR: Represented by a single circumflex ^
-Bitwise NOT: Represented by a single tilde ~
Bitshift Left
The first and most easy to grasp is the bitshift left operator. Think of it as this (referring back to the "bowl" method):
Take each stone in the bowls and move them over n amount of spaces to the left. For example:
8 bitshifted left 4 times:
|
CODE:
|
Copy / Restore :: Remove Scroll Bars
|
Select what you want to copy and in doing so you will keep the formatting when pasting it.
|
|
Would output the number 128, or in binary, "10000000".
In Lamen's terms: Double the number bitshifted left n amount of times.
Bitshift Right
Next is the bitshift right operator. It has the same concept as the bitshift left, but you would move each "stone" over n amount of spaces to the right. Example:
16 bitshifted right 4 times:
|
CODE:
|
Copy / Restore :: Remove Scroll Bars
|
Select what you want to copy and in doing so you will keep the formatting when pasting it.
|
|
Which would output 1. Or in binary, "1". 
Bitwise AND
The bitwise AND is a bit different than the bitshifts. It actually compares the number, rather than change it. For example, we'll compare the numbers 84 (1010100) and 29 (11101):
|
CODE:
|
Copy / Restore :: Remove Scroll Bars
|
Select what you want to copy and in doing so you will keep the formatting when pasting it.
|
|
This would output the number 20, or in binary "0010100".
How did I do this? Well, when comparing, you would line up your two numbers, like so:
|
CODE:
|
Copy / Restore :: Remove Scroll Bars
|
1010100 & 0011101 -------------- 0010100
Select what you want to copy and in doing so you will keep the formatting when pasting it.
|
|
You would then compare the digits. With the bitwise AND, if both digits are 1, then the solution has the value of 1 in that spot. As you see in the example, bowls "4" and "16" both have a "stone" in them in both numbers, so the solution would have stones in bowls "4" and "16". Get it?
Remember: If one number is shorter than the other, just add 0s infront of it to make them align (it's like adding a 0 to the number 8 to make it 08. 8 = 08, so it makes no difference).
Bitwise OR
Bitwise OR works the same sa AND, but it also works if only one side of the equation has a stone in the bowl. For example, we'll compare the numbers 84 and 29 again:
|
CODE:
|
Copy / Restore :: Remove Scroll Bars
|
Select what you want to copy and in doing so you will keep the formatting when pasting it.
|
|
This would output 93, or in binary "1011101".
We'll compare the digits again:
|
CODE:
|
Copy / Restore :: Remove Scroll Bars
|
1010100 | 0011101 -------------- 0010100
Select what you want to copy and in doing so you will keep the formatting when pasting it.
|
|
See how bowls "1", "4", "8", "16", and "64" have at least one stone in them between the two numbers? That means the solution will have a stone in those bowls.
Bitwise XOR
Your probably wondering how to pronounce that. It stands for Exclusive Conjunction, or in short for, Exclusive Or. It works the same as Bitwise AND and OR, but each bowl compared between each number can only contain ONE stone. WE'll compare 84 and 29 once again:
|
CODE:
|
Copy / Restore :: Remove Scroll Bars
|
Select what you want to copy and in doing so you will keep the formatting when pasting it.
|
|
This will generate the answer 73, or in binary "1001001". Let's compare the numbers:
|
CODE:
|
Copy / Restore :: Remove Scroll Bars
|
1010100 ^ 0011101 -------------- 0010100
Select what you want to copy and in doing so you will keep the formatting when pasting it.
|
|
Bowls "64", "8" and "1" contain only 1 stone between both of them in each equation, so the solution will have a stone in each of those bowls.
Bitwise NOT
The last bitwise operator is bitwise NOT. This one's pretty easy to explain. But first, here's an example with the number 9 (or in binary "1001"):
|
CODE:
|
Copy / Restore :: Remove Scroll Bars
|
Select what you want to copy and in doing so you will keep the formatting when pasting it.
|
|
This outputs the number 6, or in binary "110".
What bitwise NOT does is it checks to see of a bowl has a stone or not. If it does, it removes it, and if it doesn't, it adds one.
|
CODE:
|
Copy / Restore :: Remove Scroll Bars
|
Select what you want to copy and in doing so you will keep the formatting when pasting it.
|
|
binary "1001" turns into "0110", and after we remove the 1 at the beginning (as it makes no difference if it's there or not) we end up with "110".
So, binary is pretty easy to understand once you understand it (ok, bad pun I know). 
And, upon my exit, I leave you with a binary joke:
There are 10 types of people in the world, those who can read binary and those who can't.
Enjoy, and comments are always nice!
-Mike
Please login to rate coding articles.
Click here to register a free account with us.
|
|
Comments
|
| Please login to post comments. |
|
|
LOL i get the joke XD binary ftw
|
|
|
[i]So, binary is pretty easy to understand once you understand it (ok, bad pun I know).[/i]
A better pun would have been;
So, binary is pretty easy once you learn it bit by bit. :)
For anyone looking for practical uses of Binary check out
http://www.watchguard.com/infocenter/editorial/135183.asp
http://www.watchguard.com/infocenter/editorial/135186.asp
All in all, good article, I enjoyed it anyway.
|
|
|
good but the article title is misleading as you're not actually programming in binary, you're programming using binary, and there is alot more ground to cover.
For those wishing to program in binary take a look at this:
http://www.rohitab.com/discuss/index.php?showtopic=28221
Possibly the importance of Hex (base 16) and its involvment with Binary (base 2) would be useful and how you can quickly and easily convert between Hex and Binary.
You could have talked about the XOR function and why it's so useful with encryption alogrithms and other uses of the Bitwise operators.
Other than that its good, I might create a top up article of this which goes into more indepth computing, i.e, talking about bits etc and manipulation.
|
|
|
 |
Categories |
 |
|
|