by

Introduction to Binary, Hexadecimal, and Data Types

All Your Bases are Belong to Us (Tee Hee :))

Our number system is based on ten digits, 0 through 9, which are used to represent all numbers. This system is referred to as the base-ten or decimal numeral system. Numbers that are larger than 9 are represented with two or more digits, each occupying a position in the number that represents a power-of-ten order-of-magnitude. For example, the digits in the number one hundred and twenty-three (123) occupy positions from right to left referred to as the ones place (order of magnitude 100), the tens place (order of magnitude 101), and the hundreds place (order of magnitude 102). Thus, an equivalent way to represent this number is as follows.

1 x 102 + 2 x 101 + 3 x 100

The use of the base-ten system in the development of human mathematics may be a consequence of us having a total of ten fingers to use for counting. Other than this biological fact, there is nothing inherently better about using a base-ten system versus a base five or base thirty-five system. In fact, due to the nature of electronic circuits where information is represented by voltages that can be either high (e.g., 5 V) or low (0 V), the base-two numeral system, also called binary, is used to “talk” with electronics and process information in electronic circuits.

Binary only uses two digits, zero and one. The number 123 in binary would be written 01111011 where from right (least significant digit) to left (most significant digit) the digits occupy the 20 (one), 21 (two), 22 (four), 23 (eight), 24 (sixteen), 25 (thirty-two), 26 (sixty-four), and 27 (one hundred and twenty-eight) place. Thus, this number can also be written as:

0 x 27 + 1 x 26 + 1 x 25 + 1 x 24 + 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20

Eight-Bit Binary Numbers

The number 01111011 is referred to as an eight-bit number because it requires eight bits (a zero or one) to be written. Actually only seven bits are required since the greatest value bit is zero. An eight-bit number is also referred to as one byte in the language of computers. A four-bit number is referred to as a nibble. An eight-bit computer represents information in chunks (memory locations) consisting of eight-bits. A sixty-four-bit computer represents information in chunks of sixty-four bits. An eight-bit number can represent decimal numbers from 0 to 255 (0 to 28-1). However, sometimes one of the bits is reserved to hold a positive or negative sign. A signed eight-bit number or signed byte can therefore represent decimal numbers from -127 to 127.

Practice

Practice converting the following decimal numbers to binary. If you
get stuck, use this calculator to get you started then strive to 
understand the result. 
A) 7
B) 36
C) 97
D) 173
E) 10,589

Practice converting the following binary numbers to decimal. If you 
get stuck, use this calculator to get you started then strive to 
understand the result.
F) 11
G) 1001
H) 1100 1101
I) 1111 1111 1111 1111

Hexadecimal Numbers

Note that one nibble (four bits) can represent decimal numbers between zero and fifteen. For this reason, another popular system used in computer programming is the base sixteen or hexadecimal numeral system. The “digits” used to represent a hexadecimal number are

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

where each hexadecimal digit can represent one binary nibble.

More Practice

J) Rewrite the binary numbers in (G), (H), and (I) as hexadecimal 
numbers. Hint, convert each group of four bits to its decimal 
equivalent then assign this number the correct hexadecimal digit.

Binary Arithmetic

Arithmetic is performed on binary numbers in the same way it is performed on decimal numbers except one must carry to the next place if an operations yields a value of two (10 in binary) or higher instead of ten (10 in decimal) or higher like in the decimal system.

Even More Practice

K) Find 00111100 + 01010101
L) Find 00010011 x 00000011

Assignment

When programming, it is important to be aware of the data types your variables have. Search the ardunio.cc documentation for a list of the various data-types available in the Arduino IDE. Read about how to represent numbers in binary and hexadecimal in the Arduino IDE (see this post). I have written an Arduino sketch that accepts a user-supplied serial input consisting of a number between 0 and 255 and returns the binary equivalent as a string data-type. We will play around with this program sometime. Answer the following questions. To learn more about ASCII characters see this post and the Wikipedia article linked to here. Feel free to use Google too.

1) What range of values can an int variable have?
2) What range of values can a long variable have?
3) What data type would you use to represent a number with decimal 
places? 
4) What is the maximum number of significant digits such a number 
could have?
5) How do you store and access numbers in an array?
6) What is the ASCII character (char) equivalent of the number 75?
7) What data-type should you use to store the number 57,443?
8) What is a boolean variable?
9) Consider the following code. What value would C have?

int A = 6;
int B = 4;
int C = A/B;

10) Consider the following code. What value would C have?

int A = 6;
int B = 4;
float C = (float)A/(float)B;

Leave a Reply

Your email address will not be published.