0% found this document useful (0 votes)
5 views

Lecture 1

Uploaded by

ryuu.ducat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 1

Uploaded by

ryuu.ducat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Carnegie Mellon

Bits, Bytes, Integers and fractional


decimal numbers

N. Navet - Computing Infrastructure 1 / Lecture 1

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 1
Carnegie Mellon

Today: Bits, Bytes, and Integers


 Representing information as bits
 Representing characters
 Numeral systems (additive and positional) and basis
 Data representations in memory
 Encoding integers: unsigned and signed
 Numerical limitations of integer encodings
 Encoding fractional decimal numbers

We will cover the 3 most important representations of numbers:


✓ Unsigned encoding for positive integers
✓ Two’s-complement encoding for signed integers
✓ Floating-point encoding for real numbers

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 2
Carnegie Mellon

Everything is bits in modern computers


 Each bit is 0 or 1 – a byte is a sequence of 8 bits
 By encoding/interpreting sets of bits in various ways
▪ Computers determine what to do (instructions) both
▪ … and represent and manipulate data: numbers, strings, etc…
 Why bits? Electronic implementation is cheap and reliable
▪ Easy to store in memory with “bistable” elements (only two stable
configurations or states, corresponding to different voltages)
▪ Reliably transmitted on noisy and inaccurate wires
0 1 0

1.1V
0.9V

0.2V
0.0V
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 3
Carnegie Mellon

https://en.wikipedia.org/wiki/Character_encoding https://en.wikipedia.org/wiki/Punched_tape

Data and instructions stored on paper, not digitally

REPRESENTING CHARACTERS

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 4


Carnegie Mellon

Characters encoding
❑ ASCII (American Standard Code for Information Interchange)
❑ 127 char. including 95 printable char. – stored using 1 byte (= 8bits) per
character – ok for English but not for most languages: French (’ç’), German,
Greek, Chinese, …
❑ Unicode (industry standard developed by the Unicode consortium since 1990)
❑ Latest: over 143,00 characters covering 154 modern and historic scripts
❑ Standard defines UTF-8, UTF-16, and UTF-32 (each can represent anything
the others can represent but their size is ≠, UTF-32 char. always 4 bytes long)
❑ ex: UTF-8, dominantly used by websites (over 90%), uses one byte for the first
128 “code points” (index of the character in the table), and up to 4 bytes for
other characters. The first 128 Unicode code points are the ASCII characters,
which means that any ASCII text is also a UTF-8 text”
❑ EBCDIC (Extended Binary Coded Decimal Interchange Code) → from IBM,
disappearing How many languages in the world?
Around 6900, see
https://www.linguisticsociety.org/content/how-many-languages-are-there-world
nb: not all are written, and many rely on the same characters set

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 5


Carnegie Mellon

Representing ASCII strings char S[6] = "18213";


 Example: “null terminated string” in C/C++
▪ Represented by array of characters Memory

▪ Each character encoded in ASCII format (1 byte per character): 31 0x100


E.g: character “0” has code 0x30 - Digit i has code 0x30+i
▪ 38 0x101

▪ String should be null-terminated 32 0x102

▪ Final character = 0 (NUL character whose Ascii value is zero) 31 0x103

33 0x104
 In other programming languages 00 0x105

▪ Memory representation depends on the character set and the in hex.


programming language
▪ “null terminated string” is not at all a standard, e.g. strings can be
stored as records with a field indicating the size

Quite complex in Python! see


https://rushter.com/blog/python-strings-and-memory/

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 6
Carnegie Mellon

https://www.mathsisfun.com/numbers/numbers-numerals-digits.html

NUMERAL SYSTEMS AND BASIS


Other Types of Digits and Numerals
throughout history

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 7


Carnegie Mellon

Numeral Systems – ways to represent numbers

Positional system = the contribution of a digit to


the value of the number depends on its position

VS Positional Systems
E.g. in the decimal system (base 10), the numeral
4327 means (4×103) + (3×102) + (2×101) + (7×100)
noting that 100 = 1.

Numeral system with just 1


[Wikipedia Tally Marks]
symbol?
Additive systems
With two symbols

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 8


Carnegie Mellon

Positional Numeral Systems – different basis


❑ Base (=radix) :
▪ The number of different symbols (digits) needed to
represent any given number

❑ The larger the base, the more digits are used


▪ Base 10: 0,1,2,3,4,5,6,7,8,9 Why do people use base 10
▪ Base 2: 0,1 representation ?
▪ Base 8: 0,1,2,3,4,5,6,7
▪ Base 16 : 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
For programmers,
Hexadecimal (base 16) is a
❑ For a given number, the larger the base good tradeoff between
▪ the more symbols required base 2 (too verbose) and
▪ but the fewer digits needed for a number base 10 (not easy to
convert to base 2)

1 digit in Hexa = 4 bits

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 9


Carnegie Mellon

Notation: XY denotes string


Encoding Byte Values of digits X expressed in base Y
Assume Base 10 if not mentioned

 Byte = 8 bits
▪ Binary 000000002 to 111111112 0 0 0000
▪ Decimal: 010 to 25510 1 1 0001
2 2 0010
▪ Hexadecimal 0016 to FF16 3 3 0011
▪ Base 16 number representation 4 4 0100
5 5 0101
▪ Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’ 6 6 0110
7 7 0111
▪ FA1D37B16 in many programming languages 8 8 1000
is written as: 9 9 1001
A 10 1010
– 0xFA1D37B or B 11 1011
– 0xfa1d37b (case insensitive) C 12 1100
D 13 1101
E 14 1110
F 15 1111
Example: 13 = 2^3 + 2^2 + 2^0

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 10
Carnegie Mellon

Different basis Notation: XY denotes string


of digits X expressed in base Y

[Wikipedia Radix]

❑ In base b (b > 1), a string of digits d1 … dn denotes the


number d1∙bn−1 + d2∙ bn−2 + … + dn∙ b0, where 0 ≤ di < b.
1) Express 2010 in binary, hexadecimal and octal basis
2) What is the decimal value of 2016 , 208 and 202 ?
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 11
Carnegie Mellon

Positional Numeral Systems – different basis

Notice there are 4 rocks and


the alien has 4 fingers!

[Sanjay Kulkarni]

✓ The alien cannot understand 4 as digit 4 does not exist in its base
(4 in based 10 is expressed as ’10’ in base 4)
✓ the number of unique symbols in any base is ’10’ in that base
✓ How should the astronaut explain we are using base 10?

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 12


Carnegie Mellon

REPRESENTATION IN MEMORY

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 13


Carnegie Mellon

Machine Words
 Any given computer has a “Word Size”
▪ Nominal size of integers, memory addresses and operands of most
instructions manipulating integers

▪ Until recently, most machines used 32 bits (4 bytes) as word size


▪ Limits addresses to 4GB (232 bytes)

▪ Most machines now have 64-bit word size


▪ Potentially, could have 18 EB (exabytes) of addressable memory
▪ That’s 18.4 x 1018

▪ Machines have instructions for manipulating multiple data formats


▪ Fractions or multiples of word size. Ex: 2, 4, 8-byte integers
▪ But always an integral number of bytes!

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 14


Carnegie Mellon

Word-Oriented Memory Organization


Conceptually, memory is a very large array of bytes storing instructions
and data but organized in words 32-bit 64-bit
Bytes Addr.
 Addresses Specify Byte Words Words
Locations 0000
Addr
▪ Address of first byte in word =
0001
0000
?? 0002
▪ Addresses of successive words differ Addr
0003
by 4 (32-bit) or 8 (64-bit) depending =
0000
?? 0004
on word size Addr
=
0005
0004
?? 0006
 Note: system provides private 0007
address spaces (= the virtual 0008
Addr
0009
address space) to each “process” =
0008
?? 0010
▪ A process is a program being Addr
= 0011
executed 0008
?? 0012
▪ So, a program can work like it has a Addr
0013
=
dedicated machine 0012
?? 0014
0015
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 15
Carnegie Mellon

Byte Ordering
 So, how are the bytes within a multi-byte word ordered in
memory?
 Conventions
▪ Big Endian: Sun (obsolete), PPC Mac (obsolete), Internet protocols
▪ Least significant byte has highest address
▪ Little Endian: x86, ARM processors running Android, iOS, and Windows
▪ Least significant byte has lowest address
 Bi-endian: “Some architectures (e.g., Intel Itanium - IA-64)
feature a setting which allows for switchable endianness in data
fetches and stores, instruction fetches, or both.”
Big-endian is the most common format in data networking - fields in the
protocols of the Internet protocol suite, such as IPv4, IPv6, TCP, and UDP,
are transmitted in big-endian order.

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 16
Carnegie Mellon

✓ Different compilers & OS


Byte Ordering Example assign different locations in
memory to objects
✓ Possible to have different
memory locations at each run
 Example
▪ Variable x has 4-byte value of 0x0123456A
▪ Stored at memory address 0x100 (from 0x100 to 0x103)

Represent how variable x in stored in memory from address


0x100 on a Big Endian and Little Endian machine (least
significant byte has lowest address)

Big Endian 0x100 0x101 0x102 0x103


01
01 23
23 45
45 6A
67

Little Endian 0x100 0x101 0x102 0x103


6A
67 45
45 23
23 01
01

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 17
Carnegie Mellon

Representing Integers Decimal: 15213


long int in C is 4 bytes on 32bit CPUs and Binary: 0011 1011 0110 1101
8 bytes on 64 bit CPUs.
Better off using fixed width integer types Hex: 3 B 6 D
such as int64_t available in C99

int A = 15213; /* 4 bytes */


IA32, x86-64 Sun long int C = 15213;
6D 00 IA32 x86-64 Sun
3B 00 6D 6D 00
00 3B 3B 3B 00
00 6D 00 00 3B
00 00 6D
int B = -15213; 00
00
IA32, x86-64 Sun 00
93 FF 00
C4 FF
FF C4 Two’s complement representation
used for signed integers, introduced later
FF 93
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 18
Carnegie Mellon

ENCODING INTEGERS
A) UNSIGNED ENCODING
B) TWO’S COMPLEMENT FOR
SIGNED INTEGERS

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 19


Carnegie Mellon

Ranges for integer types

#bits needed to
store an
unsigned int ?

Range depends on the


language and word size of the machine

Integers in Python 3 are of unlimited size (capped by machine


memory) – drawback is speed as CPU instructions not directly used

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 20
Carnegie Mellon

Encoding Integers
 Commonly-used lengths for integers are 8,16,32,64 bits.
There are 2 types of integers:
 Unsigned Integers: can represent zero and positive integers.
 Signed Integers: can represent zero, positive and negative
integers. Several distinct representation schemes have been
proposed for signed integers, e.g:
▪ Sign-Magnitude representation
▪ 1's Complement representation
▪ 2's Complement representation

Modern computers all operate based on 2's


complement representation because it
allows for cheap and fast hardware
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 21
Carnegie Mellon

Notations
 We denote the vector made up of [xw−1, xw−2, . . . , x0] the
individual bits of an integer data type of w bits written in
binary notation
 Function B2U() (binary-to-unsigned) takes as input this vector
and returns its unsigned interpretation
 Similarly function B2T() (binary-to-two’s complement) returns
its signed interpretation

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 22
Carnegie Mellon

Binary vector of bits [xw−1, xw−2, . . . , x0]


Encoding Integers
Unsigned Two’s Complement
w−1 w−2
B2U(X ) =  xi 2 i
B2T (X ) = − xw−1 2 w−1
+  xi 2 i
i=0 i=0

short int x = 15213;


short int y = -15213; Sign
Bit
 C short type is assumed 2 bytes long here
Decimal Hex Binary
x 15213 3B 6D 00111011 01101101
y -15213 C4 93 11000100 10010011

 Sign Bit
▪ For 2’s complement, most significant bit indicates sign
▪ 0 for nonnegative Let’s consider integers of 4 bits, what
▪ 1 for negative is the value of 1111 in unsigned and
two’s complement encodings ?
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 23
Carnegie Mellon

Encoding Integers
Contribution of
each power of two

Example binary
strings and their value

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 24


Carnegie Mellon

Two-complement Encoding Example (Cont.)


x = 15213: 00111011 01101101
y = -15213: 11000100 10010011
Weight 15213 -15213
1 1 1 1 1
2 0 0 1 2
4 1 4 0 0
8 1 8 0 0
16 0 0 1 16
32 1 32 0 0
64 1 64 0 0
128 0 0 1 128
256 1 256 0 0
512 1 512 0 0
1024 0 0 1 1024
2048 1 2048 0 0
4096 1 4096 0 0
8192 1 8192 0 0
16384 0 0 1 16384
-32768 0 0 1 -32768
Sum 15213 -15213
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 25
Carnegie Mellon

Considering a w-bit long signed integer, what is


Numeric Ranges the numeric range [Tmin,Tmax] with two’s
complement encoding ?
 Unsigned Values
▪ UMin = 0
000…0  Two’s Complement Values
▪ UMax = 2w – 1 ▪ TMin = –2w–1
111…1 100…0
▪ TMax = 2w–1 – 1
w is the length of the vector of bits
011…1
 Other noteworthy value
▪ Minus 1
Important values for word size of 16 111…1
Decimal Hex Binary
UMax 65535 FF FF 11111111 11111111
TMax 32767 7F FF 01111111 11111111
TMin -32768 80 00 10000000 00000000
-1 -1 FF FF 11111111 11111111
0 0 00 00 00000000 00000000
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 26
Carnegie Mellon

Numeric ranges for different Word Sizes


W
8 16 32 64
UMax 255 65,535 4,294,967,295 18,446,744,073,709,551,615
TMax 127 32,767 2,147,483,647 9,223,372,036,854,775,807
TMin -128 -32,768 -2,147,483,648 -9,223,372,036,854,775,808

 Observations
▪ |TMin | = TMax + 1
▪ Asymmetric range: there
is one more negative
value than positive value
▪ UMax = (2 * Tmax) + 1

https://hpc-docs.uni.lu/getting-started/

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 27
Carnegie Mellon

Unsigned & Signed Numeric Values


X B2U(X) B2T(X)
0000 0 0
0001 1 1
0010 2 2
0011 3 3  Equivalence
0100 4 4 ▪ Same encodings for nonnegative
0101 5 5 values
0110 6 6
0111 7 7
 Uniqueness
1000 8 –8 ▪ Every bit pattern represents
1001 9 –7 unique integer value
1010 10 –6 ▪ Each representable integer has
1011 11 –5 unique bit encoding (e.g., there is
1100 12 –4 no two different encodings for
1101 13 –3 zero)
1110 14 –2
1111 15 –1

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 28
Carnegie Mellon

Conversions between types


Many programming languages like C/C++ provide two
kinds of functions:
 Binary re-interpretation of the bit field in a new

type: the bit field stay identical but how these bits
are interpreted change – also called casting
Let’s consider a variable of type unsigned int
(16 bits) assigned to FFFF. What is the result of
“casting” it into a 16-bit signed int ?
 Proper conversion: the bit field may not remain
identical after conversion (e.g., conversion from a
float to an integer)

Python only provides conversion!

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 29


Carnegie Mellon

Let u and v, two 16-bit unsigned integer variables both equal to 65535
What happens when the processor executes u := u + v ?

NUMERICAL LIMITATIONS OF
COMPUTERS – MODULO
ARITHMETIC FOR INTEGERS

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 30
Carnegie Mellon

Warning: numerical limitations of


computers
 Computers cannot represent infinite mathematical sets like
integers or real numbers
 For integers, computers use modular arithmetic.
The modulus is one more than the maximum value that
can be represented with the integer type that is used

A mod B = remainder of A divided by B

[http://www.cs.uwm.edu/classes/cs315/Bacon/Lecture/HTML/ch04.html]

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 31
Carnegie Mellon

Overflow in action

Expected behavior since


Max. int32 value = (2^31 – 1) < 1.2 10^10

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 32
Carnegie Mellon

Programming languages have various ways


to handle overflows – not always treated
as errors
“On some processors like
graphics processing units
(GPUs) and digital signal
processors (DSPs) which
“Integers are support saturation
implemented as arithmetic,
“long” integer overflowed results would be
objects of arbitrary "clamped", i.e. set to the
size in Python3 and minimum or the maximum
do normally not value in the representable
overflow” see here range, rather than wrapped
around”
[Wikipedia]

[Wikipedia - https://en.wikipedia.org/wiki/Integer_overflow]

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 33


Carnegie Mellon

Unsigned Addition
Operands: w-bit quantities u •••
thus between 0 and 2^w - 1
+v •••
“True” sum may require w+1 bits u+v •••
What is done to not exceed UAddw(u , v) •••
w bits? The highest-order bit of
the (w+1)-bit quantity is discarded
→ This results in u + v mod 2w
 Unsigned addition implements Modular Arithmetic
s = UAddw(u , v) = u + v mod 2w

Let u and v, two 16-bit unsigned integers equal to 65535


What is the result of u := u + v ?

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 34
Carnegie Mellon

Visualizing (Mathematical) Integer Addition


i.e., no overflow here
 Integer Addition Add4(u , v)
▪ 4-bit integers u, v thus Integer Addition

max value is 15
▪ Compute true sum
Add4(u , v)
32
▪ Values increase linearly 28

with u and v 24
20
16
14
12 12
8 10
8
4
0 4
6
v
0
2 2
4
6
u 8
10
12
14
0

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 35


Carnegie Mellon

Visualizing Unsigned Addition (modular arithmetic)


▪ Now, let’s assume there is 14+15 = ?
only 4 bits to store the
results → overflow will happen
Overflow
▪ With unsigned only positive Normal
overflows happen, with signed
numbers both negative and UAdd4(u , v)
positive overflows happen
▪ Overflows happen as well for
other operations like
multiplication
16
14
12

True Sum 10

2w+1 Overflow 8
6 12
14

4 10
8

2w
2
0
6 v
4
0
2 2
4
6

0 u 8
10
12
14
0

Modular Sum
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 36
Carnegie Mellon

BIT SHIFTS OPERATORS FOR


UNSIGNED INTEGERS

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 37


Carnegie Mellon

Multiplying by a power-of-two with shift to


the left u << k means u is shifted
k bits to the left, dropping off
the k most significant bits
and filling the right end with k zeros.
 Operation
▪ u << k gives u * 2k
▪ Nb: this is not a circular shift, the digits that get shifted past the end are lost
▪ Left shift of a negative signed number has implementation-defined behavior
and thus should not be used
▪ Most machines shift and add much faster than multiply
▪ Compilers optimize arithmetic operations with bit shifts automatically
 Example: u << 3 == u * 8

[00010101] << 3 ?
[01100011] << 3 ?
Nb: w=8
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 38
Carnegie Mellon

Unsigned Power-of-2 Division with Shift to the right


u >> k means u is shifted
k bits to the right, dropping off
the k least significant bits
and filling the left end with k zeros.

 Quotient of Unsigned by Power of 2


▪ u >> k gives  u / 2k 
Notations:
one has

Division (real) Computed Hex Binary


x 15213 15213 3B 6D 00111011 01101101
x >> 1 7606.5 7606 1D B6 00011101 10110110
x >> 4 950.8125 950 03 B6 00000011 10110110

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 39
Carnegie Mellon

Unsigned Power-of-2 Division with Shift to the right


u >> k means u is shifted
k bits to the right, dropping off
the k least significant bits
and filling the left end with k zeros.

 Quotient of Unsigned by Power of 2


▪ u >> k gives  u / 2k 
Notations:
one has

Division (real) Computed Hex Binary


x 15213 15213 3B 6D 00111011 01101101
x >> 1 7606.5 7606 1D B6 00011101 10110110
x >> 4 950.8125 950 03 B6 00000011 10110110

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 40
Carnegie Mellon

Arithmetic: concluding remarks


 Overflows can happen as the number of bits to represent a
number is finite
 “integer” arithmetic in computers relies on modular
arithmetic to solve this issue.
 Other operators than unsigned addition are out the scope
of the lecture

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 41
Carnegie Mellon

FRACTIONAL BINARY NUMBERS:


BINARY NUMBERS WITH A
FRACTIONAL PART
A necessary step for the understanding of floating-point numbers

Base 10 example (i.e., a decimal number):


12 + 34/100 = 1 × 10^1 + 2 × 10^0 + 3 × 10^−1 + 4 × 10^−2
the whole part is 12, the fractional part (after the decimal point) is 34/100

Nb: p/q is a fraction if both p and q are positive (with q≠0), otherwise it is a rational
number. All fractions are rational numbers.
0.25 is a fraction, while -0.25 is a rational number.

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 42


Carnegie Mellon

Decimal numbers
 A decimal number (or decimal fraction) is a number in base 10 with
a decimal point, like 0.6, 12.34, noting that 12.34 is a shorthand
way of writing 12 + 3/10 + 4/100.

 Any real numbers can be represented by a possibly infinite decimal


representation (but everything is of finite size in computers…)
 Fractional binary numbers are similar to decimal numbers but with
digits being 0 and 1 (instead of 0 to 9), positive/negative powers of
2 (instead of power of 10) and a binary point (instead of a decimal
point)
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 43
Carnegie Mellon

Fractional Binary Numbers


2i
2i-1
Encode 5+3/4
4
••• 2
1

bi bi-1 ••• b2 b1 b0 b-1 b-2 b-3 ••• b-j


1/2
1/4 •••
1/8

 Representation 2-j
▪ Bits to right of “binary point” are negative powers of 2
▪ value:

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 44
Carnegie Mellon

Fractional Binary Numbers: Examples


 Value Representation
5 + 3/4 101.112
2 + 7/8 010.1112
1 + 7/16 001.01112 ?

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 45
Carnegie Mellon

Fractional Binary Numbers (FNB): Examples


 Value Representation
5 + 3/4 101.112
2 + 7/8 010.1112
1 + 7/16 001.01112

 Observations
1. Numbers of form 0.111111…2 are just below 1.0
1/2 + 1/4 + 1/8 + … + 1/2i + … ➙ 1.0

2. Divide by 2 by shifting the binary point to the left by one position
3. Multiply by 2 by shifting the binary point to the right by one position

Check points 2 and 3 with 5+3/4

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 46
Carnegie Mellon

Representable Numbers with fractional


Rounding will happen, but it will create
binary numbers unexpected behaviors such as:
 Limitation 1.0 / 10 → 0.10000000000000001
▪ Cannot exactly represent all fractional numbers: some have infinitely
repeating bit patterns and thus cannot be represented exactly in finite size
▪ Value Representation
▪ 1/3 0.0101010101[01]…
▪ 1/5 0.001100110011[0011]…
▪ 1/10 0.0001100110011[0011]…

Note that 1/5 and 1/10 can be


represented exactly as decimal Representing 1/5 with increasing
fraction but not as fractional accuracy by lengthening
binary numbers ! the binary representation

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition [edited NN] 47

You might also like