Coder Profile - Show off your skills, get a coder profile.
 
 
 
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
  1. 1101001
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
  1. 1000001
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
  1. 8 << 4
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
  1. 16 >> 4
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
  1. 84 & 29
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
  1. 1010100 &
  2. 0011101
  3. --------------
  4. 0010100
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
  1. 84 | 29
This would output 93, or in binary "1011101".

We'll compare the digits again:
Code Copy / Restore
  1. 1010100 |
  2. 0011101
  3. --------------
  4. 0010100
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
  1. 84 ^ 29
This will generate the answer 73, or in binary "1001001". Let's compare the numbers:
Code Copy / Restore
  1. 1010100 ^
  2. 0011101
  3. --------------
  4. 0010100
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
  1. ~9
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
  1. 1001
  2. -------
  3. 0110
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


Posted By mikeMarek
Please login to rate coding articles.

Click here to register a free account with us.
Attached Image
Comments
Please login to post comments.
 
gr3nade     Posted 9 Days Ago
 
 
LOL i get the joke XD binary ftw
 
Seachmall     Posted 147 Days Ago
 
 
[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.
 
Uranium-239     Posted 157 Days Ago
 
 
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.
Page 1 of 1
More Articles By This Author
Reading And Programming In Binary
Recently Posted "Other" Articles
Reading And Programming In Binary
Cheat Sheet Pack
Recently Rated "Other" Articles
Reading And Programming In Binary
Cheat Sheet Pack
source codes Categories articles
Browse All
Business & E-Commerce (1)
Databases (1)
Design & Creativity (1)
Internet & Web Sites (1)
Life In General (2)
Operating Systems (3)
Other (2)
Programming (48)
Security (10)
Software Development (5)
Web Development (15)
search Search Inside
Other
 
 
Part of the MyPingle Network
Development Blog :: Make A Donation :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents
Version 1.44.00
Copyright © 2007 - 2008, Scott Thompson, All Rights Reserved