Chapter 04b Bits, Bytes and Number System

Chapter 4
Bits and Bytes
• Why bits?
• Representing information as bits
–Binary/Hexadecimal
–Byte representations
• Representing Expressing in C
• Simple Gates and Circuits

Why Don’t Computers Use Base 10?
Base 10 Number Representation
• That’s why fingers are known as “digits”
• Natural representation for financial transactions
– Floating point number cannot exactly represent $1.20
$1 20
• Even carries through in scientific notation
– 1.5213 X 104

Implementing Electronically
• Hard to store
– ENIAC

C ((First
st e
electronic
ect o c co
computer)
pute ) used 10
0 vacuum
acuu tubes / d
digit
gt
• Hard to transmit
– Need high precision to encode 10 signal levels on single wire
• Messy to implement digital logic functions
– Addition, multiplication, etc.

Page 1

Binary Representations
Base 2 Number Representation
• Represent 1521310 as 111011011011012

• Represent 1.2010 as 1.0011001100110011[0011]…2
• Represent 1
1.5213
5213 X 104 as 1
1.1101101101101
11011011011012 X 213

Electronic Implementation
• Easy to store with bistable elements
• Reliably transmitted on noisy and inaccurate wires
0

1

0

3.3V
2.8V
0.5V
0.0V


Introduction to Information Technology, Diplome FMIPA UGM

Most computers are digital
 Recognize only two
discrete states: on or off
 Use a binary system to
recognize two states
 Use Number system with
two unique digits: 0 and
1, called bits (short for
binary digits)

Page 2

Introduction to Information Technology, Diplome FMIPA UGM

What is a byte?




Eight bits grouped together as a unit
Provides enough different combinations of 0s and 1s to
p
256 individual characters
represent





Numbers
Uppercase
and lowercase
letters
Punctuation
marks

(ascii 3)


(ascii 5)

(ascii D)

Byte-Oriented Memory Organization
Programs Refer to Virtual Addresses
• Conceptually very large array of bytes
• Actually implemented with hierarchy of different memory types
– SRAM,
SRAM DRAM
DRAM, disk
– Only allocate for regions actually used by program
• In Unix and Windows NT, address space private to particular
“process”
– Program being executed
– Program can clobber its own data, but not that of others

Compiler + Run-Time System Control Allocation
• Where different program objects should be stored
• Multiple mechanisms: static, stack, and heap

• In any case, all allocation within single virtual address space

Page 3

Machine Words
Machine Has “Word Size”
integer-valued
valued data
• Nominal size of integer
– Including addresses
• Most current machines are 32 bits (4 bytes)
– Limits addresses to 4GB
– Becoming too small for memory-intensive applications
• High-end systems are 64 bits (8 bytes)
– Potentially address  1.8 X 1019 bytes
• Machines support multiple data formats
– Fractions or multiples of word size
– Always integral number of bytes

Word-Oriented Memory Organization

32-bit 64-bit
Words Words
Addr
=
0000

Addresses Specify Byte
Locations
• Address of first byte in word
• Addresses of successive words
differ by 4 (32-bit) or 8 (64-bit)

Addr
=
0000
Addr
=
0004

Addr

=
0008

Addr
=
0012

Page 4

Addr
=
0008

Bytes Addr.
0000
0001
0002
0003
0004
0005

0006
0007
0008
0009
0010
0011
0012
0013
0014
0015

Sizes of C Objects (in Bytes)
C Data Type

Compaq Alpha Typical 32-bit

int
long int
char
short

float
double
long double
char *

4
8
1
2
4
8
8
8

4
4
1
2
4
8

8
4

Intel IA32
4
4
1
2
4
8
10/12
4

» Or any other pointer

Introduction to Information Technology, Diplome FMIPA UGM

Values for Different Word Sizes
UMax
TMax
a
TMin

8
255
127
-128

16
65,535
32,767
3
, 6
-32,768

W
32
4,294,967,295
2,147,483,647
,
, 83,6
-2,147,483,648

C Programming
• #include
– K&R, App. B11
• Declares constants, e.g.,
– ULONG_MAX
– LONG_MAX
– LONG_MIN
• Values platform-specific

Page 5

64
18,446,744,073,709,551,615
9,223,372,036,854,775,807
9,
3,3 ,036,85 , 5,80
-9,223,372,036,854,775,808

Introduction to Information Technology, Diplome FMIPA UGM

Sign Extension Example
short int x = 15213;
int
ix = (int) x;
short int y = -15213;
int
iy = (int) y;

x
ix
y
iy

Decimal
Hex
3B
15213
15213 00 00 C4
C4
-15213
-15213 FF FF C4

Binary
6D
00111011
92 00000000 00000000 00111011
93
11000100
93 11111111 11111111 11000100

01101101
01101101
10010011
10010011

• Converting from smaller to larger integer data type
• C automatically performs sign extension

Word be ordered in memory
Conventions
• Alphas, PC’s are “Little Endian” machines
– Least significant byte has lowest address
• Sun’s, Mac’s are “Big Endian” machines
– Least
L
t significant
i ifi
t byte
b t has
h highest
hi h t address
dd

Example
• Variable x has 4-byte representation 0x01234567
• Address given by &x is 0x100
address
Big Endian

0x100 0x101 0x102 0x103

01
Little Endian

23

45

67

0x100 0x101 0x102 0x103

67

45

23

Page 6

01

data

Example: show_bytes in C
int a = 15213;
printf("int a = 15213;\n");
show bytes((pointer) &a
show_bytes((pointer)
&a, sizeof(int));

Result:
int a = 15213;
0x11ffffcb8

0x6d

0x11ffffcb9

0x3b

0x11ffffcba

0x00

0x11ffffcbb

0x00

Example: Representing Integers
int A = 15213;
int B = -15213;
long int C = 15213;

Decimal: 15213
Binary:

0011 1011 0110 1101

Hex:

3

B

6

Alpha A

Sun A

Alpha C

Sun C

6D
3B
00
00

00
00
3B
6D

00
00
3B
6D

Alpha B

Sun B

93
C4
FF
FF

FF
FF
C4
93

6D
3B
00
00
00
00
00
00

D

Two’s complement representation

Page 7

Introduction to Information Technology, Diplome FMIPA UGM

Short Int Example
• One’s complement: Invert bits for negative numbers
• Sign magnitude: Invert sign bit for negative numbers
short
o t int
te
examples
a p es
•s
15213
Unsigned
-15213 Two’s complement
-15213 One’s complement
-15213 Sign magnitude

00111011
11000100
11000100
10111011

01101101
10010011
10010010
01101101

• ISO C does not define what encoding machines use for signed
integers, but 99% (or more) use two’s complement.
• For truly portable code, don’t count on it.

Example: Representing Floats
Float F = 15213.0;
IEEE Single Precision Floating Point Representation
Hex:
Binary:

4
6
6
D
B
4
0
0
0100 0110 0110 1101 1011 0100 0000 0000
1110 1101 1011 01

15213:

Alpha F

Sun F

00
B4
6D
46

46
6D
B4
00

Page 8

Example: Representing Pointers
Alpha P

int B = -15213;
int *P = &B;

A0
FC
FF
FF
01
00
00
00

Alpha Address
1

Hex:
Binary:

F

F

F

F

F

C

A

0

0001 1111 1111 1111 1111 1111 1100 1010 0000

Sun P
EF
FF
FB
2C

Sun Address
Hex:
Binary:

E
F
F
F
F
B
2
C
1110 1111 1111 1111 1111 1011 0010 1100

Different compilers & machines assign different locations to objects

Representing Strings

char S[6] = "15213";

Strings in C
• Represented by array of characters
• Each character encoded in ASCII format
– Standard 7-bit
7 bit encoding of character set
– Other encodings exist, but uncommon
– Character “0” has code 0x30
» Digit i has code 0x30+i
• String should be null-terminated
– Final character = 0

Compatibility
• B
Byte
t ordering
d i
nott an issue
i
– Data are single byte quantities
• Text files generally platform independent
– Except for different conventions of line
termination character!

Page 9

Alpha S

Sun S

31
35
32
31
33
00

31
35
32
31
33
00

Representation Instruction Code
Encode Program as Sequence of Instructions
• Each simple operation
– Arithmetic operation
p
– Read or write memory
– Conditional branch
• Instructions encoded as bytes
– Alpha’s, Sun’s, Mac’s use 4 byte instructions
» Reduced Instruction Set Computer (RISC)
– PC’s use variable length instructions
» Complex Instruction Set Computer (CISC)
• Different instruction types and encodings for different machines
– Most code not binary compatible

Example: Representing Instructions
int sum(int x, int y)
{
return x+y;
}

Alpha sum

• For this example, Alpha & Sun
use two 4-byte instructions
– Use differing numbers of
instructions in other cases
• PC uses 7 instructions with
lengths 1, 2, and 3 bytes
– Same for NT and for Linux
– NT / Linux not binary
compatible

00
00
30
42
01
80
FA
6B

Sun sum

PC sum

81
C3
3
E0
08
90
02
00
09

55
89
E5
8B
45
0C
03
45
08
89
EC
5D
C3

Different machines use totally different instructions and encodings

Page 10

Boolean Algebra
Developed by George Boole in 19th Century
• Algebraic representation of logic
– Encode “True” as 1 and “False” as 0

And

Or

A&B = 1 when both A=1 and B=1

A|B = 1 when either A=1 or B=1

& 0 1
0 0 0
1 0 1
Not
~A = 1 when A=0

~
0 1
1 0

|
0
1

0
0
1

1
1
1

Exclusive-Or (Xor)
A^B = 1 when either A=1 or B=1, but
not both

^ 0 1
0 0 1
1 1 0

Gates and Symbols

Page 11

Diagram of a Typical Computer Circuit

1Bit compare
for Equality

0

0

1

1

+0 +1 +0 +1
Adder
half

00

01

01

10

http://lpmpjogja.diknas.go.id/kc/b/boolean/boolean.htm

The 1-ADD Circuit and Truth Table

Page 12

Introduction to Information Technology, Diplome FMIPA UGM

Full Adder Circuit

1bi
t

CI

A

B

Q

CO

0

0

0

0

0

0

0

1

1

0

0

1

0

1

0

0

1

1

0

1

1

0

0

1

0

1

0

1

0

1

1

1

0

0

1

1

1

1

1

1

1bi
t

4bi
t
http://lpmpjogja.diknas.go.id/kc/b/boolean/boolean.htm

Introduction to Information Technology, Diplome FMIPA UGM

Example

A: 0011
B: 0101

Page 13

© Christian Jacob

Chapter 4
Binary Data Representation
and
Binary Arithmetic
4.1

Binary Data Representation

4.2

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

4.3

Number System Basics
Useful Number Systems for Computers
Decimal Number System
Binary Number System
Octal Number System
Hexadecimal Number System
Comparison of Number Systems

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

Binary Logic

4.6

Binary Arithmetic

4.7

Negative Numbers and Complements
4.7.1
4.7.2
4.7.3

4.8

Floating Point Numbers
4.8.1
4.8.2
4.8.3

4.9

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

Mantissa and Exponent
Floating Point Arithmetics
Limited Precision

Representation of Strings
4.9.1
4.9.2

Representation of Characters
Representation of Strings

Chapter Overview

Chapter 4: Binary Data Representation and Arithmetic

Page 3

4.1

© Christian Jacob

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

3

2

1

0

8

4

2

1

...
2n-1

First

Back

TOC

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 4

© Christian Jacob

• The word size in any given computer is fixed.
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

Chapter 4: Binary Data Representation and Arithmetic

Page 5

© 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

Chapter 4: Binary Data Representation and Arithmetic

Page 6

4.2

© Christian Jacob

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 specific 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
n–1

Value: N B =



i

di ⋅ B = dn – 1B

n–1

+ dn – 2B

n–2

1

+ … + d1B + d0B

0

i=0
First

Back

TOC

Important Number Systems for Computers

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 7

© Christian Jacob

4.2.2 Useful Number Systems for Computers

Name

Base

dual
binary

2

0, 1

octal

8

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

Digits

Important Number Systems for Computers

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 8

© 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

=

First

Back

TOC

2 + 90 + 600 + 7000 + 40000

Important Number Systems for Computers

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 9

© 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

Chapter 4: Binary Data Representation and Arithmetic

Page 10

© Christian Jacob

Counting in Binary

First

Back

TOC

Decimal

Dual

Decimal

Dual

0

00 000

16

10 000

1

00 001

17

10 001

2

00 010

18

10 010

3

00 011

19

10 011

4

00 100

20

10 100

5

00 101

21

10 101

6

00 110

22

10 110

7

00 111

23

10 111

8

01 000

24

11 000

9

01 001

25

11 001

10

01 010

26

11 010

11

01 011

27

11 011

12

01 100

28

11 100

13

01 101

29

11 101

14

01 110

30

11 110

15

01 111

31

11 111

Important Number Systems for Computers

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 11

© 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

First

Back

TOC

=

3 · 1 + 0 · 8 + 4 · 64 + 2 · 512 + 1 · 4096

=

537910

Important Number Systems for Computers

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 12

© Christian Jacob

Counting in Octal

First

Back

TOC

Decimal

Octal

Decimal

Octal

0

00

16

20

1

01

17

21

2

02

18

22

3

03

19

23

4

04

20

24

5

05

21

25

6

06

22

26

7

07

23

27

8

10

24

30

9

11

25

31

10

12

26

32

11

13

27

33

12

14

28

34

13

15

29

35

14

16

30

36

15

17

31

37

Important Number Systems for Computers

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 13

© 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

Chapter 4: Binary Data Representation and Arithmetic

Page 14

© Christian Jacob

Counting in Hexadecimal

First

Back

TOC

Decimal

Hexadecimal

Decimal

Hexadecimal

0

00

16

10

1

01

17

11

2

02

18

12

3

03

19

13

4

04

20

14

5

05

21

15

6

06

22

16

7

07

23

17

8

08

24

18

9

09

25

19

10

0A

26

1A

11

0B

27

1B

12

0C

28

1C

13

0D

29

1D

14

0E

30

1E

15

0F

31

1F

Important Number Systems for Computers

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 15

© Christian Jacob

4.2.7 Comparison of Number Systems
Decimal

Dual

Octal

Hexadecimal

Decimal

Dual

Octal

Hexadecimal

0

00 000

00

00

16

10 000

20

10

1

00 001

01

01

17

10 001

21

11

2

00 010

02

02

18

10 010

22

12

3

00 011

03

03

19

10 011

23

13

4

00 100

04

04

20

10 100

24

14

5

00 101

05

05

21

10 101

25

15

6

00 110

06

06

22

10 110

26

16

7

00 111

07

07

23

10 111

27

17

8

01 000

10

08

24

11 000

30

18

9

01 001

11

09

25

11 001

31

19

10

01 010

12

0A

26

11 010

32

1A

11

01 011

13

0B

27

11 011

33

1B

12

01 100

14

0C

28

11 100

34

1C

13

01 101

15

0D

29

11 101

35

1D

14

01 110

16

0E

30

11 110

36

1E

15

01 111

17

0F

31

11 111

37

1F

First

Back

TOC

Important Number Systems for Computers

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 16

4.3

© Christian Jacob

Powers of 2
2N

N

First

Back

2N

N

0

1

17

131,072

1

2

18

262,144

2

4

19

524,288

3

8

20

1,048,576

4

16

21

2,097,152

5

32

22

4,194,304

6

64

23

8,388,608

7

128

24

16,777,216

8

256

25

33,554,432

9

512

26

67,108,864

10

1,024

27

134,217,728

11

2,048

28

268,435,456

12

4,096

29

536,870,912

13

8,192

30

1,073,741,824

14

16,384

31

2,147,483,648

15

32,768

32

4,294,967,296

16

65,536

33

8,589,934,592

TOC

Powers of 2

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 17

4.4

© Christian Jacob

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

Chapter 4: Binary Data Representation and Arithmetic

Page 18

© Christian Jacob

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

Step 1:

1020 :

8

= 127 Remainder: 4

127 · 8 = 1016

Step 2:

127 :

8

= 15

Remainder: 7

15 · 8 = 120

Step 3:

15 :

8

=

1

Remainder: 7

1·8=8

Step 4:

1 :

8

= 0

Remainder: 1

⇒ 102010 = 17748

First

Back

TOC

Conversions Between Number Systems

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 19

© 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
2 · 160 +

=

1F216

15 · 161 +
1 · 162
=

2 + 240 + 256

⇒ 1F216 = 49810
First

Back

TOC

Conversions Between Number Systems

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 20

© Christian Jacob

Conversions between Numbers 2n and 2m
• Conversion 2n ⇒ 21
octal ⇒ dual:

n = 3:

Replace 1 octal digit by 3 binary digits.
Example: 3 5 78 = 011 101 1112

hexadecimal ⇒ dual

n = 4:

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

Chapter 4: Binary Data Representation and Arithmetic

Page 21

© Christian Jacob

• Conversion 21 ⇒ 2n
dual ⇒ octal:

n = 3:

Replace 3 binary digits by 1 octal digit.
Example: 1011110102

=
=

101 111 0102
5

7

28

hexadecimal ⇒ dual

n = 4:

Replace 4 binary digits by 1 hexadecimal digit.

Example: 110001111100102

First

Back

TOC

=

11 0001 1111 00102

=

3

1

Conversions Between Number Systems

F

216

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 22

© Christian Jacob

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

263518

=

2

6

3

5

18

octal to binary:

= 010 110 011 101 0012

rearrange binary:

= 0010 1100 1110 10012

=

First

Back

TOC

2

C

E

Conversions Between Number Systems

916

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 23

© 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 “sufficiently” small number.

Base B ⇒ Base 10 Conversion
Example: 0.110012 = ?10
0.11001

First

Back

TOC

=

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

Chapter 4: Binary Data Representation and Arithmetic

Page 24

© Christian Jacob

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

Step i

N

Operation

R

z( –i )

1
2
3
4
5
6
7
8
9

0.19
0.38
0.76
0.52
0.04
0.08
0.16
0.32
0.64

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

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 + ε

First

Back

TOC

Multiplication!

Conversions Between Number Systems

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 25

4.5

© Christian Jacob

Binary Logic
Logical AND ( x ∧ y )
AND

0

1

0

0

0

1

0

1

OR

0

1

0

0

1

1

1

1

Logical OR ( x ∨ y )

First

Back

TOC

Binary Logic

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 26

© Christian Jacob

Logical NOT ( ¬x , negation)
x

¬x

0

1

1

0

Logical XOR (exclusive OR)
XOR

0

1

0

0

1

1

1

0

NAND

0

1

0

1

1

1

1

0

Logical NAND (negated AND)

First

Back

TOC

Binary Logic

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 27

© Christian Jacob

Examples:

AND

11001010 01000111 11110000
00110101 01110010 10101010
00000000 01000010 10100000

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

Chapter 4: Binary Data Representation and Arithmetic

Page 28

4.6

© Christian Jacob

Binary Arithmetic
Elementary Rules for addition, subtraction, and multiplication

Addition

Subtraction

Multiplication

First

Back

TOC

Operation

Result

Carry

0+0
0+1
1+0
1+1

0
1
1
0

0
0
0
1

0-0
0-1
1-0
1-1

0
1
1
0

0
1
0
0

0·0
0·1
1·0
1·1

0
0
0
1

0
0
0
0

Binary Arithmetic

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 29

© Christian Jacob

Examples:

Addition:

Subtraction:

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

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

1010
+
1110
––––––––––––––––2110

1310
1010
––––––––––––––––310

First

Back

TOC

Binary Arithmetic

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 30

© Christian Jacob

Multiplication:

111·101
––––––––––––––––––––––––
1 1 1
00 0
+
1 1 1
––––––––––––––––––––––––
1
carry:
+
1
1
carry:
+
1
carry:
1
+
1
––––––––––––––––––––––––
1 0 0 0 1 1

First

Back

TOC

Binary Arithmetic

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 31

4.7

© Christian Jacob

Negative Numbers and Complements
Integer Numbers: A possible representation
15 14

1

sign

0

value

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

First

Back

TOC

- 2n-1+1 … 2n-1-1

Negative Numbers and Complements

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 32

© Christian Jacob

Example (1): n = 3

Range:
=
=

First

Back

TOC

Sign

Value

Number

0

00

0

0

01

1

0

10

2

0

11

3

1

00

?

1

01

-1

1

10

-2

1

11

-3

Negative zero?

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

Negative Numbers and Complements

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 33

© 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)

12
-15
––––––
-3

(base 10)

• “1-1”: 1 - 1 = 1 + (-1) = 0 ?
0 001
+ 1 001
––––––––––––
1 0 1 0 (= -210)

001
001
–––––––––
000

Clearly this won´t work!
Solution: 1-Complement and 2-Complement
First

Back

TOC

Negative Numbers and Complements

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 34

© 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

Decimal

One´s Complement

Decimal

0 1001

9

1 0110

-9

1 1001

-6

0 0110

+6

0 0000

0

1 1111

-15

0 1111

15

1 0000

?

Problem!
First

Back

TOC

Negative Numbers and Complements

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 35

© 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

Decimal

Two´s Complement

Decimal

0 1001

9

1 0110 + 1 = 1 0111

-9

0 1101

3

1 0010 + 1 = 1 0011

-3

0 0000

0

1 1111 + 1 = 0 0000

0

0 1111

15

1 0000 + 1 = 1 0001

-15

1 1111

-1

¬(1 1111 - 1) =
¬(1 1110) = 0 0001

1

1 0110

-10

0 1010

10

First

Back

TOC

Negative Numbers and Complements

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 36

© Christian Jacob

Number ring for 5-digit 2-complement

10000

10

01111
011
01 10
01 101
10
0

Addition

10001
10
100 1
01
0
10

16
-16

Subtraction

10

-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

27
-5 28

11
1
11 00
10
111 1
10
11111

-4 2930
31
-3
-2
-1

First

Back

TOC

4
1

0

3

001
10
50
01
01

0
10
00 011
00 10
000
00001

10
0
1
1
11
0
11

2

00000

Negative Numbers and Complements

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 37

© Christian Jacob

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

B = 10

Example 1

Example 2

1410 - 710

910 - 1310

Complement

7:
(-7)B-1:

13:
(-13)B-1:

Addition

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

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

–––––––––

–––––––––

00111
11000
+
1
–––––––
(-7)B: 11001

7: 1 00111

First

Back

TOC

Negative Numbers and Complements

01101
10010
+
1
–––––––
(-13)B: 10011

-4:

11100

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 38

4.8

© Christian Jacob

Floating Point Numbers

4.8.1 Mantissa and Exponent
Recall the general form of numbers expressed in scientific 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 (floating point) number is:
mantissa = 21 bits

First

Back

TOC

exponent = 11 bits

Floating Point Numbers

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 39

© Christian Jacob

4.8.2 Floating Point Arithmetics
Example: 0.740 · 105 + 0.843 · 103
Operands

Compare Exponents

0.740 · 105
0.843 · 103

Adjust Exponents and Mantissae

0.740 · 105
+ 0.008 · 105

e1 - e2 = 2

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

Chapter 4: Binary Data Representation and Arithmetic

Page 40

© 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

First

Back

TOC

+

0.792 · 10-2

+

4.752 · 10-3

Floating Point Numbers

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 41

© 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

Chapter 4: Binary Data Representation and Arithmetic

Page 42

© Christian Jacob

4.8.3 Limited Precision
Note: Not all values can be represented!
Example:
- mantissa: 2 decimal digits
- exponent: 1 decimal digit
74 · 102 = 7400

• Sample number:
What is the next higher value?

75 · 102 = 7500
What about values

7400 < x < 7500?

⇒ 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

Chapter 4: Binary Data Representation and Arithmetic

Page 43

4.9

© Christian Jacob

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

Chapter 4: Binary Data Representation and Arithmetic

Page 44

© Christian Jacob

ASCII Character Encoding
D

C

D

C

D

C

D

C

D

C

D

C

D

C

D

C

0

NUL

16

DLE

32

SP

48

0

64

@

80

P

96



112

p

1

SCH

17

DC1

33

!

49

1

65

A

81

Q

97

a

113

q

2

STX

18

DC2

34



50

2

66

B

82

R

98

b

114

r

3

ETX

19

DC3

35

#

51

3

67

C

83

S

99

c

115

s

4

EOT

20

DC4

36

$

52

4

68

D

84

T

100

d

116

t

5

ENQ

21

NAK

37

%

53

5

69

E

85

U

101

e

117

u

6

ACK

22

SYN

38

&

54

6

70

F

86

V

102

f

118

v

7

BEL

23

ETB

39

´

55

7

71

G

87

W

103

g

119

w

8

BS

24

CAN

40

(

56

8

72

H

88

X

104

h

120

x

9

HT

25

EM

41

)

57

9

73

I

89

Y

105

i

121

y

10

LF

26

SUB

42

*

58

:

74

J

90

Z

106

j

122

z

11

VT

27

ESC

43

+

59

;

75

K

91

[

107

k

123

{

12

FF

28

FS

44

,

60

<

76

L

92

\

108

l

124

|

13

CR

29

GS

45

-

61

=

77

M

93

]

109

m

125

}

14

S0

30

RS

46

.

62

>

78

N

94

^

110

n

126

~

15

S1

31

US

47

/

63

?

79

O

95

_

111

o

127

DEL

Compare the Unicode character encodings …

First

Back

TOC

Representation of Strings

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 45

© 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

2

3

4

5

6

7

1

2

3

4

5

6

7

P

e

t

e

r

SP SP

C

o

m

p

u

t

e

r

2. Variable length strings
1

2

3

4

5

7

P

e

t

e

r NUL

6

n-1 n

3. Encoding with string length at the beginning

First

Back

1

2

3

4

5

6

5

P

e

t

e

r

TOC

7

8

Representation of Strings

n-1 n

Prev Next

Last

Chapter 4: Binary Data Representation and Arithmetic

Page 46

© Christian Jacob

With ASCII charcter encoding string comparisons are possible:

´AA´ <

´AD´

´AA´ <
´A1´

<

because:

101 1018 <

101 1048

´Aa´

101 1018 <

101 1418

´AA´

101 0618 <

101 1018

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

First

Back

TOC

Representation of Strings

Prev Next

Last