Clan x86

Technical (Development, Security, etc.) => General Programming => Topic started by: Joe on May 24, 2005, 08:40:15 pm

Title: Binary Tutorial
Post by: Joe on May 24, 2005, 08:40:15 pm
Bits
If you're in this forum, you probably know that each letter is 8 bits long. A bit is a single integer, 0 or 1, which is actually represented by an impulse of electricty for 1, or a lack thereof for 0, in your computers RAM. Now, how do we convert these impulses of electricty to a letter?

ASCII
All 256 characters have an ASCII value, 0 to 255. Each of the 256 integers has a binary representation too. Lets asume you've already converted your letter to Ascii.
Note, this can be done by using 'a' in the common syntax, or Asc("a") in VisualBasic, asuming you're working with the letter a.

Place Value
The 8 binary integers each have a value of their own. From left to right, these are 128, 64, 32, 16, 8, 4, 2, and 1. This may be confusing, so I'll provide an example. The number 10000000 has a value of 128, and 00100000 has a value of 32. How do we get up to 256? We need to set more of 0's to 1's.

Applying Binary
By now, you should have a pretty good idea of how binary works. Now lets apply it to some actual use. A null terminator, commonly used at the end of packets, is the ASCII character 0. To represent this in binary, we use 00000000. The character "a" has an ASCII value of 97. This is where the real fun begins.

I don't see X number for any place in binary
You're going to have to do some major subtraction here. We're working with "a", ASCII letter 97. Seeing as how 97 is under 128, we set the first bit to 0.
0XXXXXXX
Its under 64, so we set the second to 1.
01XXXXXX
Before we go any further, we need to subtract 64 from 97. We are left with 33. Seeing as how 33 is over 32, we'll set the next bit to a 1, and subtract 32.
011XXXXX
We're left with one, and we know thats not equal or bigger than anything but itself, so we don't need to check for the rest. We'll just set the last bit to a 1 and we're done.
01100001 = 97.

If anyone has any questions, feel free to ask.
Title: Re: Binary Tutorial
Post by: Sidoh on May 24, 2005, 08:56:21 pm
I'd hope no one here needs a tutorial on binary. ;)

Here's a general approach to ASCII to binary conversions (and visa-versa):
http://www.dark-wire.net/sidoh/?fuseaction=code.view&id=6
Title: Re: Binary Tutorial
Post by: Nate on May 24, 2005, 09:01:04 pm
WTF since when is binary not a two headed canary?
Title: Re: Binary Tutorial
Post by: Sidoh on May 24, 2005, 09:06:46 pm
Quote
A bit is a single integer, 0 or 1, which is actually represented by an impulse of electricty for 1, or a lack thereof for 0
Actually, that's not usually the way it works. 0 and 1 are generally represented as differently charged signals (IE -5V for 0, +5V for 1).

Also, for practical purposes, you might want to mention that the increase of the binary variables is exponentail. For each succeeding bit, its an increased power of two (with the starting point being 0).
Title: Re: Binary Tutorial
Post by: Mythix on May 25, 2005, 03:14:16 am
Comp Skills one, we were forced to deciper binary by head, and write out answers in binary onto paper, let me tell you, that was an awesome week of 1's and 0's
Title: Re: Binary Tutorial
Post by: Ergot on May 25, 2005, 11:10:17 pm
Sigh, I love adding in binary... It's so simple :/
Title: Re: Binary Tutorial
Post by: Joe on May 26, 2005, 03:22:56 am
Quote
Actually, that's not usually the way it works. 0 and 1 are generally represented as differently charged signals (IE -5V for 0, +5V for 1).
Oh, duh. I wasn't using my brain.

Lawl big time @ Miffix.
Title: Re: Binary Tutorial
Post by: rabbit on May 27, 2005, 08:30:13 am
0d is actually 001000000000b.
97d is 001011000001b

ASCII characters are noted by positive integer values, since binary is a numerical system, it has positive and negative values.  0000b preceeding a value means negative, and 0010b means positive.  Don't forget your signs!
Title: Re: Binary Tutorial
Post by: Joe on May 28, 2005, 02:16:11 am
oh man..
Title: Re: Binary Tutorial
Post by: Sidoh on May 28, 2005, 02:21:45 am
0d is actually 001000000000b.
97d is 001011000001b

ASCII characters are noted by positive integer values, since binary is a numerical system, it has positive and negative values.  0000b preceeding a value means negative, and 0010b means positive.  Don't forget your signs!

Don't you mean succeeding? :)
Title: Re: Binary Tutorial
Post by: Nate on May 28, 2005, 11:18:43 am
I think Binary is properly read Right to Left.
Title: Re: Binary Tutorial
Post by: Sidoh on May 28, 2005, 01:30:18 pm
I think Binary is properly read Right to Left.

*slaps head*

Ugg, no more posting for me at 2AM. -_-
Title: Re: Binary Tutorial
Post by: rabbit on May 29, 2005, 10:00:47 am
Joe is wrong.  Binary is read left to right, but the 4 signing bits are always first, so the sign always preceedes the number.