OSU CSE 2421
Required Reading:
Computer Systems: A Programmers Perspective, 3rd Edition, Chapter 2 thru Section 2.1.2
The C Language Representing Data
J.E.Jones
OSU CSE 2421
Have you ever thought about words that sound exactly the same, but mean *very* different things depending upon the language in which they were spoken?
https://www.daytranslations.com/blog/different- meanings/
J. E. Jones
OSU CSE 2421
False friends
The words may be similar due to them coming from the same language family or due to loan words. In some cases, they are false friends meaning the words stand for something else from what you know.
In English to use the voice, means to say something aloud. In Dutch, aloud means ancient
The English word angel means a supernatural being often represented with wings. Angel in German
translates to fishing rod and sting in Dutch.
You mean something not specific or whatever when you say any. But in Catalan, it is equivalent to year although others use the word curs.
The arm is an upper body extremity but for the Dutch it is the term used when they mean bad. But the English term bad is equivalent to bath in Dutch.
Bank could be an institution where people deposit their money, something or someone you trust or the sloping land close to a body of water. For the Dutch, bank means cough.
An outlying building in a farm is called barn, which is the term for children in Dutch. On the other hand, the English word bat refers to a flying mammal or a club used to hit a ball. In Polish, the word means, whip.
Beer in English means a bear in Dutch, while they use the term big to refer to a baby pig.
Car in a motorized vehicle, but for the French, it means because. A chariot for the English speakers is a horse-drawn vehicle or a carriage, but the French use this term to mean something smaller, like a trolley.
J. E. Jones
OSU CSE 2421
Now that weve had our cultural moment for today, lets talk about how we represent data.
J. E. Jones
OSU CSE 2421
Modern computers store and process information as two-valued signals.
Bryant & OHallaron, Computer Systems: A Programmers Perspective, 3rd Edition
You can think of these two signals as off/on or true/false or 0/1, etc.
In isolation, a single bit is not very useful. When we group bits together and apply some interpretation that gives meaning to the different possible bit patterns, however, we can represent the elements of any finite set.
Bryant & OHallaron, Computer Systems: A Programmers Perspective, 3rd Edition
Note that this means a particular bit pattern could have several different interpretations depending upon its context. Exactly like the words we were looking at just a few moments ago..
J. E. Jones
OSU CSE 2421
Most of the time, binary (0s and 1s) data is represented in blocks of 8 digits at a time since this is the smallest addressable unit.
e.g., 1 byte=8 bits, 2 bytes=16 bits, etc.
For the next few slides and for other select times when we want to discuss individual hexadecimal values, we will be using binary data in blocks of 4 digits.
When discussing 8 bits at a time, we call it a byte; when discussing 4 bits at a time, it is called a nibble.
J. E. Jones
OSU CSE 2421
Same Bit Patterns
= Different Interpretations = Different Meanings
J. E. Jones
OSU CSE 2421
DEC OCT HEX
BINARY
Symbol Description
48 060 0x30 49 061 0x31 50 062 0x32 51 063 0x33 52 064 0x34 53 065 0x35 54 066 0x36 55 067 0x37 56 070 0x38 57 071 0x39 90 0132 0x5A
00110000
00110001
00110010
00110011
00110100
00110101
00110110
00110111
00111000
00111001
0 Zero 1 One 2 Two 3 Three 4 Four 5 Five 6 Six
109 0155 0x6D
01011010
01101101
Z Uppercase Z m Lowercase m
7 Seven 8 Eight 9 Nine
J. E. Jones
OSU CSE 2421
Binary to Decimal
Unsigned = simple binary = B2U
BINARY B2U
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
1010 10
1011 11
1100 12
1101 13
1110 14
1111 15
All digits represent a positive power of 2 NO NEGATIVE NUMBERS
0101 = 0*23+1*22+0*21+1*20 = 5
1111 = 1*23+1*22+1*21+1*20 = 15
1110 = 1*23+1*22+1*21+0*20 = 14
1001 = 1*23+0*22+0*21+1*20 = 9
We can represent any positive integer value we want
if we use enough bits.
For some number of bits w, we can represent all integer values between 0 and 2w-1.
If w=4, then all integers between 0 and 15
If w=10, then all integers between 0 and 1023
When we use the C language keyword unsigned, we are saying to interpret bits in this manner.
J. E. Jones
OSU CSE 2421
Binary to Decimal
Signed = twos complement = B2T
BINARY B2T 0000 0
Mostsignificantdigit(left-mostbit)representsthe
sign bit 0001
Remainder of digits represent positive powers of 2
ANY POSITIVE NUMBER: SAME AS B2U
1111 = -1*23+1*22+1*21+1*20 = -8 + 7 = -1
1110 = -1*23+1*22+1*21+0*20 = -8 + 6 = -2
1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7
1001 = -1*23+0*22+0*21+1*20 = -8 + 1 = -7
Another way, if sign bit = 1, then its a negative number and to get the magnitude of that number, invert all bits and add 1, then make sign negative 1010 changes to 0101+1 = 0110
1000 -8
0110=6,sofinalvalueis-6
Wecanrepresentanyintegervaluewewantifweuseenough
1001 -7 1010 -6 1011 -5 1100 -4 1101 -3 1110 -2 1111 -1
bits.
For some number of bits w, we can represent all integer values between -2w-1 and 2w-1-1.
If w=4, then all integers between -8 and 7
If w=10, then all integers between -512 and 511
WhenwedeclareintegertypevariablesintheC programming language, this is the default way to interpret bits.
J. E. Jones
OSU CSE 2421
0 2w-1
Unsigned values
-2w-1 0 2w-1-1
Same number of bits w, just different interpretation.
Signed values
J. E. Jones
OSU CSE 2421
Large binary numbers are cumbersome to read, so hexadecimal digits offer a convenient way to represent binary data.
BINARY HEX
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
Each digit in a hexadecimal integer represents four binary bits.
Think of hexadecimal digits as a type of binary shorthand
It takes two hexadecimal digits together to represent a byte 1000
1001 9
A single hexadecimal digit represents a decimal value between 0 and 15, so letters A to F (lowercase also valid) represent decimal values in the range 10 through 15. Since F is 15, then consider them as unsigned values.
1010 A
1011 B
1100 C
1101 D
1110 E
1111 F
In hexadecimal, each digit position represents a power of 16.
0010 0110 1011 1111 = 0x26BF = 2*163+6*162+B*161+F*160
8
J. E. Jones
OSU CSE 2421
Just like those words we looked at a few moments ago that changed meaning depending upon the language we wished to use, these 4-bit binary chunks can represent different values depending upon how we choose to interpret them.
BINARY HEX B2U B2T 0000 0 0 0 0001 1 1 1 0010 2 2 2 0011 3 3 3 0100 4 4 4 0101 5 5 5 0110 6 6 6 0111 7 7 7 1000 8 8 -8 1001 9 9 -7 1010 A 10 -6 1011 B 11 -5 1100 C 12 -4 1101 D 13 -3 1110 E 14 -2 1111 F 15 -1
We can use hexadecimal values to represent 4 binary bits. This typically makes them easier to read
Note that the binary value 1010 can represent: Hexadecimal A (or hexadecimal a)
Decimal 10
Decimal -6
It depends upon how we wish to interpret those 4 bits.
J. E. Jones
OSU CSE 2421
Suppose you are given the hexadecimal number : 0x173A
Convert to binary format by expanding each hexadecimal digit, as follows:
Hexadecimal 1 7 3 A
Binary 0001 0111 0011 1010
This gives the binary representation:
0001011100111010 or 0001 0111 0011 1010
Puttingspacesbetweeneachnibbleforreadabilityinthisclassisacceptable(and, sometimes, downright needful to keep your eyes from crossing).
J. E. Jones
OSU CSE 2421
Suppose you are given the binary number : 11110010101101
Starting from the right side, split the number into groups of 4 bits each (if the number of bits is not a multiple of 4, the leftmost group will have fewer than 4 bits):
Binary 11 1100 1010 1101
If the left-most group has fewer than 4 digits, add leading 0s if you wish the binary to represent an unsigned number, then pattern match each nibble with the appropriate hex digit.
Binary 0011 1100 1010 1101
Hexadecimal 3 C A D This gives the hexadecimal representation:
0x3CAD
If the left-most group has fewer than 4 digits, add leading, 1s or 0s depending upon value of MSB, if you wish the binary to represent a signed number, then pattern match each nibble with the appropriate hex digit.
Binary 1111 1100 1010 1101
Hexadecimal F C A D
This gives the hexadecimal representation: 0xFCAD
J. E. Jones
OSU CSE 2421
To convert from decimal to base b, divide the decimal number by b, and write the remainders (which will be between 0 and b 1), until the quotient is zero.
Then, write the remainders in order from the last remainder obtained to the first remainder obtained.
Example:
convert (221)10 to base 2 (binary) (see the next slide)
J. E. Jones
OSU CSE 2421
Example: Convert (221)10 to binary
Quotient Remainder 2 221 1
2 110 0 2 55 1 2 27 1 2 13 1 260 231 211
0
Write the remainders in order from the last one obtained, to the first one obtained: (221)10 = (11011101)2 = 0000 1101 1101
This expresses the binary representation of the original decimal number.
Why should we ultimately represent decimal 221 with a minimum of 12 binary digits?
We need at least 8 bits to represent 221, but, when looking at the value in 8 binary
digits, we really cant tell whether we should be interpreting the value as unsigned or
signed since the most significant bit is 1, so we should used AT LEAST 9 digits.
Using a number of binary digits to represent a value that is a multiple of 4, makes it
easier to convert from binary to hex
J. E. Jones
OSU CSE 2421
Example: Convert (743)10 to hexadecimal
Hexadecimal is considered base 16.
Note: Since remainders can have values from 0 to base 1, that is, 0 to 15 in this case, we use F for 15, E for 14, D for 13, C for 12, B for 11, and A for 10
Quotient Remainder 16 743 7
16 46 E 16 2 2
0
Therefore, if we write the remainders in order from the last one obtained to the first one obtained, we have:
(743)10 = (2E7)16 = 0x2E7
We only need 3 hexadecimal digits to represent 743 because the hexadecimal 2 interprets to 0010 in binary, so there is no confusion with respect to how the value should be interpreted. If the most significant hexadecimal digit was 8 or higher, then including a leading 0 hex digit would decrease confusion with respect to the value the hex digits represent. Of course, adding a leading 0 (e.g.,0x02E7) doesnt change its value and clearly defines a 16-bit value.
J. E. Jones
OSU CSE 2421
We can convert by multiplying the value of each digit, dm, by dm, for m from 0 (least significant digit) to n-1 (most significant digit) for an n digit number, then summing all the products obtained.
J. E. Jones
OSU CSE 2421
Problem: Convert (5377)8 to decimal.
Solution:
(5377)8 =5*83 +3*82 +7*81 +7*80
=5*512+3*64+7*8 +7*1 = 2,560 + 192 + 56 + 7
= 2,815
J. E. Jones
OSU CSE 2421
Problem: Convert (5377)16 to decimal.
Solution:
(5377)16 =5*163 +3*162 +7*161 +7*160
=5*4096+3*256+7*16 +7*1 = 20,480 + 768 + 112 + 7
= 21,367
J. E. Jones
OSU CSE 2421
Given 0x42, calculate Binary representation
B2U
B2T
Given 0x86, calculate Binary representation
B2U
B2T
J. E. Jones
OSU CSE 2421
Given 0x42, calculate
Binary representation: 0b 0100 0010
B2U:=1*26 +1*21 =64+2=66
B2T: =1*26 +1*21 =64+2=66(becausemsb0)
Given 0x86, calculate
Binary representation B2U
B2T
J. E. Jones
OSU CSE 2421
Given 0x42, calculate
Binary representation: 0b 0100 0010
B2U:=1*26 +1*21 =64+2=66
B2T: =1*26 +1*21 =64+2=66 (because msb = 0)
Given 0x86, calculate
Binary representation: 0b 1000 0110 B2U:=1*27 +1*22 +1*21 =128+4+2=134
B2T: =-1*27 +1*22 +1*21 =-128+4+2=-122 (because msb = 1)
J. E. Jones
OSU CSE 2421
These few slides give the basics.
Well get a bit more involved in the 2nd half of the semester
J. E. Jones
Reviews
There are no reviews yet.