Welcome to the RDI-Board Community.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Donate Now Goal amount for this month: 100 EUR, Received: 16 EUR (16%)
Donate to support this site...

Results 1 to 2 of 2
  1. #1
    Junior Member Teacher
    Join Date
    Apr 2004
    Posts Thanks / Likes

    Default Learning Binary and Hexadecimal

    Learning Binary and Hexadecimal


    Whether you have been programming for a few months or a few years, chances are you have came across the terms binary (bin for short) and hexadecimal (hex for short) at some point. For the programmers that have begun their careers in the years of late, these enigmatic topics are considered known only to well-seasoned programmers. In contrast, some of the older, more experienced programmers were shown this to be a fundamental concept when programming for computers. This shift in thought results in less programmers learning binary and hex nowadays.

    So, you may ask, with all the abstraction of today's high level programming languages and libraries, why should one bother with learning all of this binary and hex mumbo jumbo anyway? Well, to truly understand the eclectic nature of a computer (data storage, memory, TCP/IP, debugging, cryptography, bit-planes, compression, etc.) and the "how's" and "why's", an understanding of binary and hex is required. The truth is, virtually everything dealing with computers can be traced back to bits, bytes, memory, and CPU register addressing. Obviously, knowing the lingo a computer speaks will put you a step ahead.

    What exactly is binary and hexadecimal? It may sound like an easy question, but do you really know? Unfortunately, sometimes things are assumed known in the programming community, and nobody bothers asking. When I was first learning, someone had to tell me just as I am telling you now. So, if you are unsure, then binary and hex are simply different counting systems.

    Yup, that's it! Pretty fancy huh? Oh yeah, what's a counting system? Well, it's the way we - as humans - count varying amounts. How many ears do you have - two? You are using a counting system to be able to represent that amount.

    All systems of counting and writing ever used contain what we call glyphs to represent values, whether we are counting with roman numerals or ancient hieroglyphics. Even today, we still use glyphs to symbolize meaning just as we did back in ancient times. The difference being we understand what each picture or object represents.

    For instance, to represent a value of five the way you have always done, you will most likely use an Latin picture resembling this: 5, but in Egyptian hieroglyphics, you may see a picture of an arch: . Most of us, however, are just not used to seeing it done this way anymore. Perhaps, if we went back in time, the 5 would look odd to the general populous.

    The system you've been counting with all your life is called decimal. It has the prefix dec, which literally means ten. In the decimal counting system, we have only ten Latin characters (glyphs) to work with. To represent a value in decimal, we are limited to ten choices: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. However, by using a combination of these characters, we can create any value or amount imaginable. That is why we have counting systems in the first place, and it is much easier to remember what several characters in a working system mean as opposed to memorizing billions of different characters for every value ever needed.
    Extra: Why have you been counting in decimal your whole life? For my theory, look at how many fingers you have. Many children learning how to count, add, subtract, etc., have been taught to use their hands for assistance. It would seem logical that whether or not this would directly be related, the ten-based mentally spawned by our fingers and toes nudged us in that direction.
    The Basics

    First, we need to cover something you already learned in the first grade - how to count to ten. You might say, "no sweat, been doing it all my life" - right? Well, therein lies the problem for most people when learning binary or hexadecimal. You have been counting so long that you do not need to even think about numbers anymore - you just know the numbers. It helps to remember what you were taught as a child and to almost unlearn what you understand about the way you use numbers to count. The following will introduce you to a long-lost concept:

    Me: How much is a dozen?
    You: Duh, it's twelve!
    Me: OK, how much is twelve?
    You: Huh? Twelve is twelve, are you nuts?
    Me: Yes, but how much is twelve, as a first
    grade teacher would say to a pupil?
    You: Unless she's stupid, she'd say twelve.
    Me: Nope, if she's teaching a child how to count, she should
    have said, "Twelve is one set of tens and two ones."

    Did a light bulb just turn on? Is it all coming back to you? Good! Remembering the basic principles of counting that you learned a long time ago is paramount. Please stay in that mindset for the remainder of this article!

    Do you recall the concept of decimal places? They serve as containers to represent greater values. Every decimal place in a number to the left of the decimal point is ten greater than the previous one. With the number 123 (one hundred twenty-three), you should notice three decimal places.

    As I mentioned earlier, in decimal (also called base 10 by some), we have only ten characters to work with: 0-9. Each one represents a different value. Also, it is noteworthy to mention that when using Latin characters in this manner, it is commonplace to refer to them as digits; therefore, throughout the rest of this document, we will do so as well.

    Now, if we started counting at zero and stopped at nine, then ten digits would suffice and we would never need to use decimal places, but that's certainly not the case. To represent a value greater than nine, we need place holders (think of them as seats) that say, "If that digit parks its butt in this spot, then it's worth more than its face value alone."

    Take the dozen example from above. Twelve is more than nine, so if we want to count past nine, we must move over to the next decimal place and put the digit 1 there to indicate ten greater. We then follow it with a zero so everyone knows it belongs in the second decimal place and not the first. Now we have the amount ten, and it's easy to count to eleven, twelve, and so on.

    Let's illustrate this idea. Also, for clarity's sake in this example, we will separate the decimal places with a pipe character.

    1 | 2 | 3

    Instead of seeing 123 as one big chunk, think of it as one set of hundreds, two sets of tens, and three ones. Man, if your first grade teacher could see you now! Now, let's look at the illustration again, but with labels for each decimal place.

    Hundreds | Tens | Ones
    1 | 2 | 3

    Note the decimal place values, they are always ten greater than the one before it. Guess what, that notion right there is the cornerstone of every counting system on the planet, so pay close attention as I repeat it. In base 10, every decimal place is ten greater than the previous one!

    Decimals places always exist, regardless if we use them or not. If we wanted to name more decimal places, we could have, and we would still have the same number.

    Hundred Thousands | Ten Thousands | Thousands | Hundreds | Tens | Ones
    0 | 0 | 0 | 1 | 2 | 3

    It's important to fully understand the idea behind 0 at this point. Remember, 0 has no value in itself and is only meant to indicate "no value." You cannot use 0 to represent an amount - only decimal places! Remember, where a digit sits in a number shows the true value of a digit, not the digit itself.

    The decimal places are always there, but the last nonzero digit in a number is where the values of decimal places are no longer taken into consideration when calculating what the entire number represents. Thus, it is understood that any leading zeros do not need to be shown in a given number. It is safe to say 000123 equals 123. Likewise, since leading zeros do not matter, you could also say 123 = 00000000000000000000000000000123. It's much easier to assume the zeros.
    Only Time Will Tell

    This is a supplementary section intended to sink in the ideas behind what we are talking about. If the above information makes perfect sense to you, then feel free to skip along to the next section. But, if you would like that extra pinch of explanation or are just curious as to what the title means in this section, then please continue reading.

    We have been using the concept of placeholders (decimal places are placeholders in decimal) practically all of our lives, most likely without even realizing it. In decimal, the placeholders go by tens, but what if we went by sixty instead? Think about hours and minutes here.

    Let us compare hours to tens, and minutes to ones. The difference is, with time, you have sixty minutes to an hour, and not ten ones to a ten like you would if we were simply counting in decimal.

    Take 1:59 PM, for example. We all know that when the next minute strikes, it will be 2:00 PM and not 1:60 PM. To get to 2:00 PM, we add to the hours placeholder and reset the minutes placeholder. Why? Because it caps off after sixty minutes just like decimal will cap off after nine, we add to the next placeholder to represent the greater amount.
    Extra: It's been said that teaching someone hex before binary is better, and in my experience, that's true. I suppose the concepts involved are easier to grasp if we add to the amount of working characters we are familiar with instead of taking them away. Once you have a grasp on hex and the ideas behind it, we will then proceed to binary to make these concepts concrete.

    Mathematically speaking, the prefix hex means six, and dec means ten. So, what does the prefix hexadec stand for in the word hexadecimal? You guessed it: sixteen. What this evaluates to is that essentially in hex, you have sixteen digits to work with as opposed to only ten as in decimal.

    Where did the other six glyphs come from? Well, it was decided to use letters from the alphabet to represent them. I suppose that it looked better to the creators of hexadecimal than a bunch of new squiggly pictures to teach the world with. In hex, we have all these new characters to work with: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. Count 'em up; that is sixteen digits!
    Extra: Even though we borrowed from the alphabet for the extra characters, we still refer to the letters as digits because in this sense, they are simply glyphs used to represent a particular value, as do zero through nine.

    To get the ball rolling, let's count from zero to ten in hex. Ready? This should look mostly familiar, with a twist at the end.

    0 1 2 3 4 5 6 7 8 9 A

    What's that you say? Why is A after 9? Well, that's the character chosen to represent the value of one greater than nine. The reason we didn't use two placeholders (two digits) to indicate the number ten was because we didn't need to. Think about it, in decimal, we have ten digits. In hex, we have sixteen. What happened in decimal is that we ran out of glyphs to use at nine. We had no more doodads to indicate a higher value, so we were forced to take advantage of the available decimal places (placeholders). With hex though, we still have more pictures, characters, glyphs, or whatever you want to call them that can represent a value. Therefore, we do not need to use a new placeholder.

    Can you guess what twelve is in hex? If you said C, you're right! If you didn't, then take a moment to stop and review a bit. The letters are digits that mean an amount. Just like you have decimal places in decimal, you have placeholders in hex. But, you don't have to use them until you run out of digits to work with.

    Now, let's count to fifteen in hex.

    0 1 2 3 4 5 6 7 8 9 A B C D E F

    As you may have guessed, F equals fifteen. But, what if we want to count to sixteen instead? Then, we would face the same issue we did back in decimal when we counted past nine. We would run out of digits to use - F is where it stops in hex. But, the same exact concept of decimal places (placeholders) also exists in hex. We have invisible placeholders we can use. They are also considered to be always present, but as long as there is no value in them, they are rarely shown.

    To solve the problem of no more digits, we place a 1 in the next placeholder over.

    In decimal, each decimal place (seat) is ten greater than the previous. Well, in hex, it is sixteen! Starting to see a similarity here? Using the pipe character again to separate placeholders - as in our first example - let's write the number sixteen in hex.

    1 | 0

    Remember, this should be read as, "one set of sixteen and zero ones." In hex, each placeholder is always sixteen times greater than the previous placeholder, thus it is also referred to as base 16.

    There is a direct correlation between the amount of digits in a counting system and the value of its placeholders. In decimal, every placeholder is times ten because of the ten digits we can use. In hex, every placeholder is times sixteen because we have sixteen digits to use. That's the nature of the game. Placeholders come into action when digits run out; consequently, they will always be the number of digits in your counting system times greater than the previous placeholder.

    To solidify this, let's take a look at a number in hex and decimal, with labeled placeholders. Notice how the same digits are used, but they are interpreted differently under an alternate counting system.

    Decimal = | Tens | Ones
    Hex = | Sixteens | Ones
    Digits = | 2 | 0

    Here, we have the number 20. But, this can represent two different values depending on if we are using decimal or hex. In decimal, it is the same as saying (2 x 10) + (0 x 1). The parentheses can be thought of as representing placeholders. With every new placeholder, we have the base number (ten for decimal; sixteen for hex) times the position of the placeholder.

    If I wanted to show the number zero for instance, using decimal, with five placeholders, I could say: (0 x 10,000) + (0 x 1,000) + (0 x 100) + (0 x 10) + (0 x 1). Notice how the placeholders increment in steps of ten? That will always be consistent in decimal (base 10)!

    Now, let's take the number 20 again, but this time we shall translate it as hex: (2 x 16) + (0 x 1). Look at the placeholders now. They are incrementing in steps of sixteen. This will always be the case in hexadecimal (base 16). Can you guess what 20 in hex is in decimal? If you said 32, then give yourself a pat on the back. Even if the characters look the same, when you change the values of the placeholders, you change the value of the entire number.

    Here's one of my favorite illustrations. Let's use a different twist and count to the number thirty in both decimal and hex side-by-side.

    Decimal | Hex Decimal | Hex Decimal | Hex
    ------------- ------------- -------------
    1 | 1 11 | B 21 | 15
    2 | 2 12 | C 22 | 16
    3 | 3 13 | D 23 | 17
    4 | 4 14 | E 24 | 18
    5 | 5 15 | F 25 | 19
    6 | 6 16 | 10 26 | 1A
    7 | 7 17 | 11 27 | 1B
    8 | 8 18 | 12 28 | 1C
    9 | 9 19 | 13 29 | 1D
    10 | A 20 | 14 30 | 1E

    I hope, by now, you see a pattern emerging. Look at the decimal number 26 in the chart. Can you tell me why the hex equivalent is 1A? If you can, give yourself a pat on the back. If it's not that clear at the moment, don't worry. Take a break and come back to reread the hexadecimal section of this document again. Just think about sets of sixteen's (* 16) + (* 1) for the placeholders, and not ten with using more digits for each seat. It will eventually make sense if you think it out.

    To become better at understanding hex or any counting system, for that matter, you must practice. So, as a side project, here are two exercises to try.

    1. Convert the following numbers to hex. 45, 7, 52, 23, 81, 100, 25, and 1.

    Hint: The chart above answers to some of the numbers.
    2. Why does the decimal number 255 equal the hex number FF?

    Hint: (F x 16) + (F x 1)


    The prefix bi means two, of course, so as you may have guessed, binary is also called base 2. That means, we have only two digits to work with: 0 and 1. And every placeholder is only two times greater than the previous one. We have no letters to mess with this time around!

    At the heart of it all, computers only understand numbers. By using electric currents, they can mimic the character (glyph) concept with different states: on and off. The presence of electricity is assumed to be a 1, and the absence is assumed to be a 0. Thus, the pattern of electric jolts a computer plays with actually means something. But, we, as people, cannot write with electricity, so in the event we want to show you what those crazy computers are saying in binary, we still use characters to show the values.
    Extra: Just like hexadecimal is often referred to as hex for short, binary is also called bin. They are common, shorthand abbreviations that are widely accepted in the programming community.

    OK, let's get the whip cracking already, and dive straight in to working with binary. Unlike last time, I'm going to count in decimal and binary first, and then explain later. I'll be using the same concepts, just under a different counting system. The idea behind this is to see something strange at first, and then apply your newly learned concepts to have it become familiar to you. Hopefully, this will help reinforce the concepts behind the using of the different counting systems.

    Decimal | Bin Decimal | Bin Decimal | Bin
    -------------- -------------- --------------
    0 | 0 4 | 100 8 | 1000
    1 | 1 5 | 101 9 | 1001
    2 | 10 6 | 110 10 | 1010
    3 | 11 7 | 111 11 | 1011

    Just like 9 is the magic digit in decimal, 1 is the magic digit in binary, because we only have two to choose from in the first place. So, after we've counted to just one, we no longer have any more digits to use. We have to resort to taking advantage of the placeholders. The key is to realize that in binary (base 2), every placeholder is two times greater than the previous one.

    Using the same type of chart as above, let's compare a number in decimal, hex, and binary. For the example, we'll use the digits 10. As above, each counting system produces unique values.

    Decimal = | Tens | Ones
    Hex = | Sixteens | Ones
    Binary = | Twos | Ones
    Digits = | 1 | 0

    In decimal, everyone knows 10 equals ten. In hex, 10 is equal to sixteen. Now, in binary, since we go by two's, 10 equals two. It's the same as saying, "one set of twos and zero ones" because of the value our placeholders represent.

    In binary, since you only have two digits, every time you count two numbers higher, you have to use a placeholder. This happens in decimal and hex too, but in binary, it happens more frequently because there are so few digits to work with. Think about the number 999 in decimal. When you count to a higher number, a new, empty placeholder is needed because all our digits in all the currently used placeholders are filled up. So, we move on to 1,000, and now the first placeholders have been cleared again, and the three trailing zeros still show the 1 belongs in the thousands placeholder.

    Don't forget the importance of zeros! Leading zeros are ignored, but not trailing zeros. You can't do anything with 0 except to reserve a placeholder. The only available digit in binary with any real value is 1. After it's used, it's time for a new placeholder to help out.

    If you look back at the chart above that counts to eleven in binary, take notice that in every two steps, the next available placeholder is filled with 1. And yes, 0 means the placeholder is still free. It only serves the purpose of showing where the digits with a value need to go (remember the zeros concept).

    Once again, here's a chart that counts from zero to six in binary, but this time, it shows several placeholders (instead of just assuming them) and separate them for illustration.

    Thirty-Twos | Sixteens | Eights | Fours | Twos | Ones
    0 | 0 | 0 | 0 | 0 | 0
    0 | 0 | 0 | 0 | 0 | 1
    0 | 0 | 0 | 0 | 1 | 0
    0 | 0 | 0 | 0 | 1 | 1
    0 | 0 | 0 | 1 | 0 | 0
    0 | 0 | 0 | 1 | 0 | 1
    0 | 0 | 0 | 1 | 1 | 0

    Take a moment to think about the points that we just discussed. Now, by using the charts as your reference, can you tell me what 1100 in binary is in decimal? If you said twelve, you're right. If you said something else, remember the formula: (1 x Cool + (1 x 4) + (0 x 2) + (0 x 1).

    To become better at understanding binary, you need to practice again. Here are two more exercises to try. By the time you get through these, you should know the basic concepts involved.

    1. Convert the following numbers to binary: 4, 15, 22, 11, 30, 8, and 1.

    Hint: The charts above contain answers for some of the numbers.
    2. Why does the binary number 10101 equal the decimal number 21?

    Hint: (1 x 16) + (0 x Cool + (1 x 4) + (0 x 2) + (1 x 1)

    Extra: Now that we have answered the "what", we should revisit the "why" we use binary. Computers communicate with numbers. It represents those numbers with electric states in binary. Each bit can show a state. 0 is no electric current, and 1 means a current is present. To simplify the matter, when a computer gets two successive jolts (11 in binary), it knows it is dealing with the number three.

    Congratulations, whether you realize it or not, you just learned the basics of hex and binary. If you take a step back for a moment, you may realize all the letters, zeros, and ones nonsense now actually have some meaning to it. In time, they won't even appear random anymore.

    To sharpen your newfound skills, you have to practice them - even with larger numbers. I kept them small in this document for the sake of clarity and illustration. Don't forget though, the more you practice your skills, the better you become with them.

    If you are using Windows, open up the calculator program (Start Menu - Programs - Accessories - Calculator, or if you have a Windows key on your keyboard, type ˙+R, type "calc", and hit Enter). Set the program View to Scientific, and you'll be able to work with and convert between decimal, hex, and binary numbers.

    Once you're a seasoned pro with hex and binary, you can also use the same exact placeholder and digit concept to count in any system in the world that you want, like ternary (base 3) or octal (base Cool.
    Extra: As a bonus, G. Filkov wrote an application, called HexIt, in WTL, that allows you to manipulate binary numbers bit-by-bit and perform several bitwise operations on them, all while seeing the equivalent hex and signed/unsigned integer values of the number. It can be downloaded from the link at the top of this article.
    Credits & History
    Article's Author - Jeremy Falcon
    Inspiration for "Extra" sections - Joe Woodbury
    Spotted the B = 12 typo - Lucas A. Hardbarger
    Suggested the use of time - jyodak1
    Suggested the Win+R shortcut - Jörgen Sigvardsson
    Author of the HexIt application - filkov
    Spotted the 7 ≠ 6 typo - eXodus
    Arabic numeral correction - Naya2005

    * 2003-04-28 - Ver. 1.0 released.
    * 2003-06-10 - Ver. 1.1 released.
    * 2006-02-24 - Ver. 1.2 released.


  2. #2
    RDI - Board Default Avatar


    Dear! Nice info about Learning binary...



Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Back to Top