★ Computers For Morons | Binary - Decimal ★

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
9 posts Page 1 of 1
Contributors
User avatar
Nery
Co-Admin Moderator
Co-Admin Moderator
Posts: 1117
Joined: Mon Sep 07, 2009 8:11 pm

[center]The Basics of Binary and Decimal[/center]


  • What is a Binary that you mentioned in the title?
Binary numeration systems use combinations of Ones (1) and Zeros (0) to process information where 1 = On and 0 = Off. Basically Binary is called BIT which practically simplifies the name itself.
  • What are the BITS and Bytes?
BIT (Binary Digit) is the smaller unit of information in a computer. It can be Ones (1) and Zeros (0), it is the basic of the basics Binary numeration system, such as I said in the above topic.
Bytes represents a combination of 8 BITS, simplifying:
Code: Select all
1 Byte = 8 BITS
2 Bytes = 16 BITS
3 Bytes = 24 BITS
(...)
Bytes are also simplified with Kilobytes, Megabytes, Gigabytes and Terabytes.
Code: Select all
1 Kilobyte (KB) = 1024 Bytes
1 Megabyte (MB) = 1024 KBytes
1 Gigabyte (GB) = 1024 MByte
1 Terabyte (TB) = 1024 GByte
1 Byte = 8 BITs ----> 256 Possible Combinations of 1 and 0

Now, moving to a more complex topic...
  • Converting Decimal to Binary
Each number you see on your screen is basically a combination of BITS (1 and 0) that allows the computer processing it and transform them in Information. Information is basically what you see in your screen, its the output of the computer's BITs processing, without a computer to process the BITs the only thing you would see was a bunch of 1 and 0...

Before moving on to the conversion take a look to this:
Code: Select all
0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
7 = 0111
8 = 1000
9 = 1001
Basically there are only 10 Decimal Digits existing in computer's language. 10, 11, 12, etc are combinations of the decimal digits, for example 11 is two ones (1) combined... In the table above you can see the representation of each one of the 10 decimal numbers in BITS.

Ok, enough with bullshit, its time to learn how you can convert those numbers to Binary:
Steps:
1) Effectuate successive divide operations by 2 until you get a 1.
2) Regroup the last quotient to all the rests of the division found by inversed order.

Example:
Image

As you can see, the rests of that operations plus the last quotient combined shows you what's the Decimal "20" in Binary!
The way I got those rests was: for example, you can't divide 5 per 2, right? Then, subtract 1 to the 5 and store it, you'll get a 4, with the 4 you can now divide the 4 for 2, but always remember that rest. After completing the operation just add the "1" to the rests, and there you go!

In the operation above:
Code: Select all
20(10) = 10100(2)
I hope you understood!
  • Converting Binary to Decimal
To convert a Binary combination to Decimal you must understand the following:
For example, we're trying to convert 1001 to Decimal...
Code: Select all
- Pick the first Digit of that combination and represent it as a potency with base 2 and index 1
- Pick the second Digit of the combination and represent it as a potency with base 2 and index 0
- Pick the third Digit of the combination and represent it as a potency with base 2 and index 0
- Pick the fourth Digit of the combination and represent it as a potency with base 2 and index 1
That makes sense, doesn't it?

For a better understanding:
Pick the digit number X from the combination and represent the potency of base 2 and index X-1

That isn't enough! To truly converting it to Decimal you'll have to add the partial multiplications done between the digit and the potency attributed to it...
We're trying to convert "10100" to Decimal:
10100(2) = 20(10)
1 x 24 + 0 x 23 + 1 x 22 + 0 + 0 x 20
After doing that operation we'll get the number "20"

It's a little hard to understand at the beginning, but after training some operations to convert binary to decimal you'll have mastered it.

This may help you in future applications you may want to do.
User avatar
Martin
Supreme VIP
Supreme VIP
Posts: 369
Joined: Sat Aug 01, 2009 12:26 pm

I Have Made One Program Like This

Martin64 :D
Image
User avatar
Nery
Co-Admin Moderator
Co-Admin Moderator
Posts: 1117
Joined: Mon Sep 07, 2009 8:11 pm

Good job ^^
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

I might understand this when I get older, you lost me at the "Ok, enough with bullshit, its time to learn how you can convert those numbers to Binary:" and under :P
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

I like to make the computer work out the bits and bytes for me
Code: Select all
        Dim number As Int32 = 510 'example

        Dim bytes As Byte() = BitConverter.GetBytes(number)
        MsgBox("Your number takes up " & bytes.Length & " bytes")

        Dim bits As BitArray = New BitArray(bytes)
        MsgBox("That's " & bits.Length & " bits")

        Dim readable As String = ""

        Dim time As Integer = 0
        For i As Integer = 0 To bits.Length - 1
            time += 1

            Dim temp As Integer = Convert.ToInt32(bits(i)) 'just used to store a 1 or 0
            readable += temp.ToString()

            If time = 8 Then
                time = 0
                readable += " " 'seperator per byte
            End If
        Next
        MsgBox(readable)
And this converts the binary string back to a number again:
Code: Select all
        Dim readable As String = "01111111 10000000 00000000 00000000"
        Dim strbytes As String() = readable.Split(" "c)

        Dim bits As BitArray = New BitArray(strbytes.Length * 8)

        Dim bitpos As Integer = 0
        For i As Integer = 0 To strbytes.Length - 1
            For i2 As Integer = 0 To 7
                bits(bitpos) = strbytes(i)(i2).ToString()
                bitpos += 1
            Next
        Next

        Dim bytes(strbytes.Length - 1) As Byte
        bits.CopyTo(bytes, 0)

        Dim number As Int32 = BitConverter.ToInt32(bytes, 0)

        MsgBox(number)
User avatar
Cheatmasterbw
Coding God
Coding God
Posts: 1506
Joined: Fri Jan 01, 2010 2:30 pm

how would i convert from binary to decimal? (in vb)
http://www.megaapps.tk/
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

You can use the above code but just change Int32 to Single.
User avatar
Cheatmasterbw
Coding God
Coding God
Posts: 1506
Joined: Fri Jan 01, 2010 2:30 pm

thanks
http://www.megaapps.tk/
User avatar
Zulf
Serious Programmer
Serious Programmer
Posts: 441
Joined: Fri Jun 11, 2010 7:46 am

Nery wrote:
Ok, enough with bullshit.
ENOUGH WITH THIS WHAT?!
Image
9 posts Page 1 of 1
Return to “Tutorials”