You are on page 1of 46

Christian Jacob

Chapter 4 Binary Data Representation and Binary Arithmetic


4.1 4.2 Binary Data Representation Important Number Systems for Computers
4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 Number System Basics Useful Number Systems for Computers Decimal Number System Binary Number System Octal Number System Hexadecimal Number System Comparison of Number Systems

4.3

Powers of 2
Chapter Overview

Christian Jacob

4.4

Conversions Between Number Systems


4.4.1 4.4.2 Conversion of Natural Numbers Conversion of Rational Numbers

4.5 4.6 4.7

Binary Logic Binary Arithmetic Negative Numbers and Complements


4.7.1 4.7.2 4.7.3 Problems of Signed Number Representation 1-Complement ((B-1)-Complement) 2-Complement (B-Complement)

4.8

Floating Point Numbers


4.8.1 4.8.2 4.8.3 Mantissa and Exponent Floating Point Arithmetics Limited Precision

4.9

Representation of Strings
4.9.1 4.9.2 Representation of Characters Representation of Strings

Chapter Overview

Page 3

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.1

Binary Data Representation


Bit: smallest unit of information yes / no, on / off, L / 0, 1 / 0, 5V / 0V Byte: group of 8 bits --> 28 = 256 different states Word: the number of bits (word length) which can be processed by a computer in a single step (e.g., 32 or 64) --> machine dependent Representation:
n-1 ... 2n-1 8 4 2 1 3 2 1 0

First

Back

TOC

Prev Next

Last

Page 4

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

The word size in any given computer is xed. Example: 16-bit word every word (memory location) can hold a 16-bit pattern, with each bit either 0 or 1. How many distinct patterns are there in a 16-bit word? Each bit has 2 possible values: 0 or 1 1 bit has 2 distinct patterns: 0, 1 With 2 bits, each position has 2 possible values: 00, 01, 10, 11 22 = 4 distinct bit patterns

First

Back

TOC

Prev Next

Last

Page 5

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

With 3 bits, again each position can be either 0 or 1: 000 001 010 011 100 101 110 111

23 = 8 distinct bit patterns

In general, for n bits (a word of length n) we have 2n distinct bit patterns.

NOTE: What these bit patterns mean depends entirely on the context in which the patterns are used.

First

Back

TOC

Prev Next

Last

Page 6

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.2

Important Number Systems for Computers

4.2.1 Number System Basics


Number systems, as we use them, consist of - a basic set Z of digits (or letters); example: Z = { 0, 1, 2, , 9 } - a base B = Z (how many digits); example: B = Z = 10 A number is a linear sequence of digits. The value of a digit at a specic position depends on its assigned meaning and on its position. The value of a number N is the sum of these values. Number: N B = d n 1 d n 2 d 1 d 0 with word length n
n1

Value: N B =

di B = dn 1B

n1

+ dn 2B

n2

+ + d1B + d0B

i=0
First Back TOC Important Number Systems for Computers Prev Next Last

Page 7

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.2.2 Useful Number Systems for Computers

Name dual binary octal

Base 2 0, 1

Digits

0, 1, 2, 3, 4, 5, 6, 7

decimal

10

0, 1, 2, 3, 4, 5, 6, 7, 8, 9

hexadecimal sedecimal

16

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

First

Back

TOC

Important Number Systems for Computers

Prev Next

Last

Page 8

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.2.3 Decimal Number System


Z = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ; B = 10 Each position to the left of a digit increases by a power of 10. Each position to the right of a digit decreases by a power of 10. Example: 4769210 = 2 100 + 9 101 + 6 102 + 7 103 + 4 104 = 2 + 90 + 600 + 7000 + 40000

First

Back

TOC

Important Number Systems for Computers

Prev Next

Last

Page 9

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.2.4 Binary Number System


Z = { 0, 1 } ; B = 2 Each position to the left of a digit increases by a power of 2. Each position to the right of a digit decreases by a power of 2. Example: 101110012 = 1 20 + 0 21 + 0 22 + 1 23 + 1 24 + 1 25 + 0 26 + 1 27
First Back TOC

= 1 + 8 + 16 + 32 + 128
Important Number Systems for Computers

= 18510
Prev Next Last

Page 10

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

Counting in Binary
Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Dual 00 000 00 001 00 010 00 011 00 100 00 101 00 110 00 111 01 000 01 001 01 010 01 011 01 100 01 101 01 110 01 111 Decimal 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Dual 10 000 10 001 10 010 10 011 10 100 10 101 10 110 10 111 11 000 11 001 11 010 11 011 11 100 11 101 11 110 11 111

First

Back

TOC

Important Number Systems for Computers

Prev Next

Last

Page 11

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.2.5 Octal Number System


Z = { 0, 1, 2, 3, 4, 5, 6, 7 } ; B = 8 Each position to the left of a digit increases by a power of 8. Each position to the right of a digit decreases by a power of 8. Example: 124038 = 3 80 + 0 81 + 4 82 + 2 83 + 1 84 = = 3 1 + 0 8 + 4 64 + 2 512 + 1 4096 537910

First

Back

TOC

Important Number Systems for Computers

Prev Next

Last

Page 12

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

Counting in Octal
Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Octal 00 01 02 03 04 05 06 07 10 11 12 13 14 15 16 17 Decimal 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Octal 20 21 22 23 24 25 26 27 30 31 32 33 34 35 36 37

First

Back

TOC

Important Number Systems for Computers

Prev Next

Last

Page 13

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.2.6 Hexadecimal Number System


Z = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F } ; B = 16 Each position to the left of a digit increases by a power of 16. Each position to the right of a digit decreases by a power of 16. Example: FB40A16 = 10 160 + 0 161 + 4 162 + 11 163 + 15 164 = =
First Back TOC

10 1 + 0 16 + 4 256 + 11 4096 + 15 65536 1,029,13010


Important Number Systems for Computers Prev Next Last

Page 14

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

Counting in Hexadecimal
Decimal Hexadecimal Decimal Hexadecimal

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F

First

Back

TOC

Important Number Systems for Computers

Prev Next

Last

Page 15

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.2.7 Comparison of Number Systems


Decimal Dual Octal Hexadecimal Decimal Dual Octal Hexadecimal

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

00 000 00 001 00 010 00 011 00 100 00 101 00 110 00 111 01 000 01 001 01 010 01 011 01 100 01 101 01 110 01 111

00 01 02 03 04 05 06 07 10 11 12 13 14 15 16 17

00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

10 000 10 001 10 010 10 011 10 100 10 101 10 110 10 111 11 000 11 001 11 010 11 011 11 100 11 101 11 110 11 111

20 21 22 23 24 25 26 27 30 31 32 33 34 35 36 37

10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F

First

Back

TOC

Important Number Systems for Computers

Prev Next

Last

Page 16

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.3

Powers of 2
N 2N N 2N

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
First Back TOC

1 2 4 8 16 32 64 128 256 512 1,024 2,048 4,096 8,192 16,384 32,768 65,536

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

131,072 262,144 524,288 1,048,576 2,097,152 4,194,304 8,388,608 16,777,216 33,554,432 67,108,864 134,217,728 268,435,456 536,870,912 1,073,741,824 2,147,483,648 4,294,967,296 8,589,934,592
Powers of 2 Prev Next Last

Page 17

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.4

Conversions Between Number Systems

4.4.1 Conversion of Natural Numbers


Base 10 Base B Conversion We use the metod of iterated division to convert a number N 10 of base 10 into a number N B of base B . 1. Divide the number N 10 by B : whole number N & remainder R . - Take N as the next number to be divided by B . - Keep R as the next left-most digit of the new number N B . 2. If N is zero then STOP, else set N 10 = N and go to step 1.

First

Back

TOC

Conversions Between Number Systems

Prev Next

Last

Page 18

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

Example: 102010 = ? 8 Number to be converted: N 10 = 1020 ; target base B = 8

Step 1:

1020 :

= 127 Remainder: 4

127 8 = 1016

Step 2:

127 :

= 15

Remainder: 7

15 8 = 120

Step 3:

15 :

Remainder: 7

18=8

Step 4:

1 :

= 0

Remainder: 1

102010 = 17748

First

Back

TOC

Conversions Between Number Systems

Prev Next

Last

Page 19

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

Base B Base 10 Conversion We use the metod of iterated multiplication to convert a number N B of base B into a number N 10 of base 10. We simply add the contribution of each digit. Example: 1F216 = ?10 Number to be converted: N 16 = 1F2 ; target base B = 10 1F216 = 2 160 + 15 161 + 1 162 = 2 + 240 + 256

1F216 = 49810
First Back TOC Conversions Between Number Systems Prev Next Last

Page 20

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

Conversions between Numbers 2n and 2m Conversion 2n 21 n = 3: octal dual: Replace 1 octal digit by 3 binary digits. Example: 3 5 78 = 011 101 1112

n = 4:

hexadecimal dual Replace 1 hexadecimal digit by 4 binary digits.

Example: 1 F 216 = 0001 1111 00102

First

Back

TOC

Conversions Between Number Systems

Prev Next

Last

Page 21

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

Conversion 21 2n n = 3: dual octal: Replace 3 binary digits by 1 octal digit. Example: 1011110102 = = 101 111 0102 5 7 28

n = 4:

hexadecimal dual Replace 4 binary digits by 1 hexadecimal digit.

Example: 110001111100102

= =

11 0001 1111 00102 3 1 F 216

First

Back

TOC

Conversions Between Number Systems

Prev Next

Last

Page 22

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

Conversion 2n 2m 2n 21 2m Example: octal hexadecimal

263518 octal to binary:

18

= 010 110 011 101 0012

rearrange binary:

= 0010 1100 1110 10012

916

First

Back

TOC

Conversions Between Number Systems

Prev Next

Last

Page 23

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.4.2 Conversion of Rational Numbers


Note: An exact conversion of rational numbers R 10 R B or R B R 10 is not always possible. We want: R 10 = R B + , where is a sufciently small number.

Base B Base 10 Conversion Example: 0.110012 = ?10 0.11001 = = = = 1 2-1 + 1 2-2 + 0 2-3 + 0 2-4 + 1 2-5 1 0.5 + 1 0.25 + 0 0.125 + 0 0.0625 + 1 0.03125 0.5 + 0.25 + 0.03125 0.7812510
Conversions Between Number Systems Prev Next Last

First

Back

TOC

Page 24

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

Base 10 Base B Conversion Example: 0.1910 = ?2 with k = 9 bit precision

Step i 1 2 3 4 5 6 7 8 9

Operation 0.19 2 = 0.38 0.38 2 = 0.76 0.76 2 = 1.52 0.52 2 = 1.04 0.04 2 = 0.08 0.08 2 = 0.16 0.16 2 = 0.32 0.32 2 = 0.64 0.64 2 = 1.28

z( i )

0.19 0.38 0.76 0.52 0.04 0.08 0.16 0.32 0.64

0.38 0.76 0.52 0.04 0.08 0.16 0.32 0.64 0.28

0 0 1 1 0 0 0 0 1

0.1910 = 0.0011000012 +

Multiplication!

First

Back

TOC

Conversions Between Number Systems

Prev Next

Last

Page 25

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.5

Binary Logic
Logical AND ( x y ) AND 0 1 Logical OR ( x y ) OR 0 1 0 0 1 1 1 1 0 0 0 1 0 1

First

Back

TOC

Binary Logic

Prev Next

Last

Page 26

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

Logical NOT ( x , negation) x 0 1 Logical XOR (exclusive OR) XOR 0 1 0 0 1 1 1 0


x

1 0

Logical NAND (negated AND) NAND 0 1 0 1 1 1 1 0

First

Back

TOC

Binary Logic

Prev Next

Last

Page 27

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

Examples: 11001010 01000111 11110000 00110101 01110010 10101010 00000000 01000010 10100000

AND

NAND

11001010 01000111 11110000 00110101 01110010 10101010 11111111 10111101 01011111

OR

11001010 01000111 11110000 00110101 01110010 10101010 11111111 01110111 11111010

XOR

11001010 01000111 11110000 00110101 01110010 10101010 11111111 00110101 01011010

First

Back

TOC

Binary Logic

Prev Next

Last

Page 28

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.6

Binary Arithmetic
Elementary Rules for addition, subtraction, and multiplication Operation Addition 0+0 0+1 1+0 1+1 0-0 0-1 1-0 1-1 00 01 10 11 Result 0 1 1 0 0 1 1 0 0 0 0 1 Carry 0 0 0 1 0 1 0 0 0 0 0 0

Subtraction

Multiplication

First

Back

TOC

Binary Arithmetic

Prev Next

Last

Page 29

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

Examples:

Addition: 1010 + 1011 carry: 1 0 1 1 + 10111 10101

Subtraction: 1101 1010 carry: 1 0 1 0 -1 110 0011

1010 + 1110 2110

1310 1010 310

First

Back

TOC

Binary Arithmetic

Prev Next

Last

Page 30

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

Multiplication:

111101 1 1 1 00 0 + 1 1 1 carry: 1 + 1 carry: 1 + 1 carry: 1 + 1 1 0 0 0 1 1

First

Back

TOC

Binary Arithmetic

Prev Next

Last

Page 31

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.7

Negative Numbers and Complements


Integer Numbers: A possible representation
15 14 1 0

sign

value

Number = sign & value sign = 0 +, positive number sign = 1 -, negative number Range of integer numbers for n bits: - 2n-1+1 2n-1-1

First

Back

TOC

Negative Numbers and Complements

Prev Next

Last

Page 32

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

Example (1): n = 3

Sign 0 0 0 0 1 1 1 1

Value 00 01 10 11 00 01 10 11

Number 0 1 2 3 ? -1 -2 -3
Negative zero?

Range: = =

- 23-1+1 23-1-1 - 22+1 22-1 - 4 + 1 4 -1 = -3 ... +3

First

Back

TOC

Negative Numbers and Complements

Prev Next

Last

Page 33

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.7.1 Problems of Signed Number Representation


Given 2 binary numbers: one positive, one negative 0 0 1 1 0 0 (base 2) + 1 01111 1 1 1 0 1 1 (= -2710) 1-1: 1 - 1 = 1 + (-1) = 0 ? 0 001 + 1 001 1 0 1 0 (= -210) 001 001 000 12 -15 -3 (base 10)

Clearly this wont work! Solution: 1-Complement and 2-Complement


First Back TOC Negative Numbers and Complements Prev Next Last

Page 34

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.7.2 1-Complement (( B-1 )-Complement)


We represent negative numbers by negating the positive representation of the number (and vice versa): any 1 is changed to a 0 any 0 is changed to a 1

Example: Binary 0 1001 1 1001 0 0000 0 1111 Decimal 9 -6 0 15 Ones Complement 1 0110 0 0110 1 1111 1 0000 Decimal -9 +6 -15 ?

Problem!
First Back TOC Negative Numbers and Complements Prev Next Last

Page 35

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.7.3 2-Complement ( B -Complement)


Positive numbers are represented as usual. Negative numbers are created by - negating the positive value and - adding one (this avoids the negative zero). Examples: Binary 0 1001 0 1101 0 0000 0 1111 1 1111 1 0110 Decimal 9 3 0 15 -1 -10 Twos Complement 1 0110 + 1 = 1 0111 1 0010 + 1 = 1 0011 1 1111 + 1 = 0 0000 1 0000 + 1 = 1 0001 (1 1111 - 1) = (1 1110) = 0 0001 0 1010 Decimal -9 -3 0 -15 1 10

First

Back

TOC

Negative Numbers and Complements

Prev Next

Last

Page 36

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

Number ring for 5-digit 2-complement

10000
Addition

01111 011 01 10 01 101 10 0

16 -16

10001 10 100 1 01

10
10

Subtraction

-14-15 15 14 1 -13 13 1 10 -12 18 17 0 12 1 101 1 -11 20 19 0 11 0 1 0 10 1 -10 21 10 0 10111 22 01 -9 23 9 010 positive 8 01000 11000 -8 24 negative numbers numbers -7 25 7 001 1 0 11 0 1 1 -6 26 6

10

10 0 111 1 10 11111

10

10 0 1 1 11 0 11
11

11

10

27 -5 28

-4 2930 31 -3 -2 -1 0

4 1 2 3

001 10 50 01 01

00000

0 10 00 011 00 10 000 00001

First

Back

TOC

Negative Numbers and Complements

Prev Next

Last

Page 37

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

With the 2-complement, subtractions can be performed as simple additions: Example: 5 digit precision, base 10

Example 1 B = 10 Complement 1410 - 710 7: (-7)B-1: 00111 11000 + 1 (-7)B: 11001

Example 2 910 - 1310 13: (-13)B-1: 01101 10010 + 1 (-13)B: 10011

Addition

14: 01110 +(-7)B: + 11001

9: 01001 +(-13)B: + 10011

7: 1 00111

-4:

11100

First

Back

TOC

Negative Numbers and Complements

Prev Next

Last

Page 38

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.8

Floating Point Numbers

4.8.1 Mantissa and Exponent


Recall the general form of numbers expressed in scientic notation: 3.141592653589793238462643383279502884197 6.02 x 1023 Printed out by a computer these numbers usually look like this: 3.1415e0 6.02e23 This representation saves space and reduces redundancy. Hence, the usual format for a 32-bit real (oating point) number is:
mantissa = 21 bits exponent = 11 bits

First

Back

TOC

Floating Point Numbers

Prev Next

Last

Page 39

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.8.2 Floating Point Arithmetics


Example: 0.740 105 + 0.843 103
Operands Compare Exponents Adjust Exponents and Mantissae

0.740 105 0.843 103

e1 - e2 = 2

0.740 105 + 0.008 105 Result: 0.748 105

Addition: 1. Identify mantissae and exponents 2. Compare exponents 3. Adjust exponents if necessary 4. Adjust the shorter mantissa 5. Add mantissae 6. Normalize mantissa 7. Adjust exponent
First Back TOC Floating Point Numbers Prev Next Last

Page 40

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

Example: Multiplication 1. Multiply of mantissae 2. Normalize mantissae 3. Adjust exponents 4. Add exponents

Example: (0.792 105) (0.116 10-3) Step 1: multiplication of the mantissae: 0.792 0.116 = 0.792 10-1 + + 0.792 10-2 4.752 10-3

First

Back

TOC

Floating Point Numbers

Prev Next

Last

Page 41

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

Step 1 (cont.): step-by-step multiplication of the mantissae 0.792 10-1 0.792 10-1 0.871 10-1 + 0.792 10-2 0.079 10-1 0.918 10-1

+ 0.4752 10-2

0.047 10-1

Step 2: Add exponents 5 + (-3) + (-1) = 1

Result: precision)in the mantissa) 0.918 101 (with 3 digits of precision


First Back TOC Floating Point Numbers Prev Next Last

Page 42

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.8.3 Limited Precision


Note: Not all values can be represented! Example: - mantissa: 2 decimal digits - exponent: 1 decimal digit Sample number: What is the next higher value? 75 102 = 7500 What about values 7400 < x < 7500? 74 102 = 7400

They cannot be represented!!! Remedy, but not a perfect solution: Increase the number of mantissa digits.

First

Back

TOC

Floating Point Numbers

Prev Next

Last

Page 43

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.9

Representation of Strings

4.9.1 Representation of Characters

Usually:

single characters are represented by 1 byte different standards: ASCII1, EBCDIC2

Currently:

internationalization with Unicode a single character is represented by 2 bytes

1. ASCII: American Standard Code for Information Interchange 2. EBCDIC: Extended Binary Coded Decimal Interchange Code (derived from punch card standards)

First

Back

TOC

Representation of Strings

Prev Next

Last

Page 44

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

ASCII Character Encoding


D 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 C NUL SCH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR S0 S1 D 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 C DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US D 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 C SP ! # $ % & ( ) * + , . / D 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 C 0 1 2 3 4 5 6 7 8 9 : ; < = > ? D 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 C @ A B C D E F G H I J K L M N O D 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 C P Q R S T U V W X Y Z [ \ ] ^ _ D 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 C a b c d e f g h i j k l m n o D 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 C p q r s t u v w x y z { | } ~ DEL

Compare the Unicode character encodings

First

Back

TOC

Representation of Strings

Prev Next

Last

Page 45

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

4.10 Representation of Strings


Strings are represented by sequences of characters ( usually: byte sequence) 1. Fixed length strings (example: length = 7)
1 P 2 e 3 t 4 e 5 r 6 7 1 C 2 o 3 m 4 p 5 u 6 t 7 e r

SP SP

2. Variable length strings


1 P 2 e 3 t 4 e 5
6

n-1 n

r NUL

3. Encoding with string length at the beginning


1 5 2 P 3 e 4 t 5 e 6 r
7

n-1 n

First

Back

TOC

Representation of Strings

Prev Next

Last

Page 46

Chapter 4: Binary Data Representation and Arithmetic

Christian Jacob

With ASCII charcter encoding string comparisons are possible:

AA < AA < A1 <

AD Aa AA

because:

101 1018 < 101 1018 < 101 0618 <

101 1048 101 1418 101 1018

Hence, sorting of strings becomes easy: Compare their numerical representations!

First

Back

TOC

Representation of Strings

Prev Next

Last

You might also like