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

Number_System

The document provides an overview of number systems, focusing on binary, decimal, and hexadecimal representations, as well as their conversions. It explains the significance of bits and bytes in data storage, introduces primitive data structures like integers and real numbers, and discusses signed and unsigned integers. Additionally, it covers the concept of two's complement for representing negative numbers in binary format.

Uploaded by

mejuliaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Number_System

The document provides an overview of number systems, focusing on binary, decimal, and hexadecimal representations, as well as their conversions. It explains the significance of bits and bytes in data storage, introduces primitive data structures like integers and real numbers, and discusses signed and unsigned integers. Additionally, it covers the concept of two's complement for representing negative numbers in binary format.

Uploaded by

mejuliaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

Number System

Storage Representation

 Bits – It is a measure of uncertainty which represents the

presence of a two-state condition, as exemplified by an on-

off condition or a true-false condition.

 No. Of Bits required to represent ‘p’ number of uncertainties

𝑛
H=- 𝑖=1 𝑝𝑖 ∗ 𝑙𝑜𝑔2(𝑝𝑖)
Bit and Byte
 A bit is the smallest unit of data stored in a computer and it has a value of 0 or 1

 It’s like a switch, on (1) or off (0)

 In computers, bits are stored electronically in RAM and auxiliary storage devices

by two-state digital circuits

 The storage device itself doesn’t know what the bit pattern represents, but

software (application software, operating system, and I/O device firmware)

stores and interprets the pattern

 That is, data is coded then stored and when retrieved it is decoded

 A byte is a string of 8 bits and is called a character when the data is text
Bits, Bytes, and Words
 A bit is a single binary digit (a 1 or 0).

 A byte is 8 bits

 A word is 32 bits or 4 bytes

 Long word = 8 bytes = 64 bits

 Quad word = 16 bytes = 128 bits

 Programming languages use these standard number of bits when organizing


data storage and access.

 What do you call 4 bits?


(hint: it is a small byte)
4
Number Systems
 The on and off states of the capacitors in RAM can be thought of as the
values 1 and 0, respectively.

 Therefore, thinking about how information is stored in RAM requires


knowledge of the binary (base 2) number system.

 Let’s review the decimal (base 10) number system first.

5
The Decimal Number System
• The decimal number system is a positional number system.
• Example:
Value 5 6 2 1 1 X 100 = 1
Position 4 3 2 1 2 X 101 = 20
Address 103 102 101 100 6 X 102 = 600
5 X 103 = 5000

6
The Decimal Number System (con’t)
• The decimal number system is also known as base 10. The values of the
positions are calculated by taking 10 to some power.

• Why is the base 10 for decimal numbers?

o Because we use 10 digits, the digits 0 through 9.

7
The Binary Number System
• The binary number system is also known as base 2. The values of
the positions are calculated by taking 2 to some power.

• Why is the base 2 for binary numbers?

o Because we use 2 digits, the digits 0 and 1.

8
The Binary Number System (con’t)
• The binary number system is also a positional numbering system.

• Instead of using ten digits, 0 - 9, the binary system uses only two digits,
0 and 1.

• Example of a binary number and the values of the positions:

1 0 0 1 1 0 1

26 25 24 23 22 21 20

9
Converting from Binary to Decimal
1 0 0 1 1 0 1 1 X 20 = 1
26 25 24 23 22 21 20 0 X 21 = 0
1 X 22 = 4
20 = 1 24 = 16 1 X 23 = 8
21 = 2 25 = 32 0 X 24 = 0
22 = 4 26 = 64 0 X 25 = 0
23 = 8 1 X 26 = 64
7710

10
Converting from Binary to Decimal (con’t)

Practice conversions:

Binary Decimal

11101
1010101
100111

11
Converting From Decimal to Binary (con’t)
• Make a list of the binary place values up to the number being converted.

• Perform successive divisions by 2, placing the remainder of 0 or 1 in each


of the positions from right to left.

• Continue until the quotient is zero.

• Example: 4210

25 24 23 22 21 20

32 16 8 4 2 1

1 0 1 0 1 0

12
Converting From Decimal to Binary (con’t)
Practice conversions:

Decimal Binary

59
82
175

13
Working with Large Numbers
• 0101000010100111 = ?

• Humans can’t work well with binary numbers; there are too many digits to
deal with.

• Memory addresses and other data can be quite large. Therefore, we


sometimes use the hexadecimal number system.

14
The Hexadecimal Number System
• The hexadecimal number system is also known as base 16. The
values of the positions are calculated by taking 16 to some power.

• Why is the base 16 for hexadecimal numbers ?

• Because we use 16 symbols, the digits 0 and 1 and the letters A


through F.

15
The Hexadecimal Number System (con’t)
Binary Decimal Hexadecimal Binary Decimal Hexadecimal
0 0 0 1010 10 A
1 1 1 1011 11 B
10 2 2 1100 12 C
11 3 3 1101 13 D
100 4 4 1110 14 E
101 5 5 1111 15 F
110 6 6
111 7 7
1000 8 8
1001 9 9
16
The Hexadecimal Number System (con’t)
 Example of a hexadecimal number and the values of the positions:

3 C 8 B 0 5 1
166 165 164 163 162 161 160

17
Example of Equivalent Numbers
Binary: 1 0 1 0 0 0 0 1 0 1 0 0 1 1 12

Decimal: 2064710

Hexadecimal: 50A716

Notice how the number of digits gets smaller as the base increases.

18
Binary Representation
 Why binary representation (as suppose to decimal or octal,
etc..)?

◦ Because the devices that store and manage the digital data are far
less expensive and complex for binary representation.

◦ They are also far more reliable when they have to represent one out
of two possible values.

◦ Because the electronic signals are easier to maintain if they carry


only binary data.
Binary Representation
 One bit can be either 0 or 1. Therefore, one bit can represent only two things.

 To represent more than two things, we need multiple bits. Two bits can
represent four things because there are four combinations of 0 and 1 that
can be made from two bits: 00, 01, 10,11.

 In general, n bits can represent 2n things because there are 2n combinations


of 0 and 1 that can be made from n bits. Note that every time we increase the
number of bits by 1, we double the number of things we can represent.
Data Representation in Memory
Data Representation
 Symbols

 Roman Numbers

 Positional system – radix ten or decimal system

◦ 133 = 1 X 102 + 3 X 101 + 3 * 100

 Binary Number System

◦ 10011 = 1 * 24 + 0 * 23 + 0 * 22 + 1*21 + 1*20 = 19


Data Representation
 Octal

◦ 5378  5 * 82 + 3 * 81 + 7 * 80 = 351 (base 10)

 HexaDecimal

◦ Ranges from 0 to F
Primitive Data Structure
 Basic representation of any data.

 Integer

◦ A natural number, the negative of a natural number, and 0.

◦ So an integer number system is a system for ‘counting’ things in a simple


systematic way

 Real Numbers

◦ Integer + floating decimal values

 Character
Basic Operations on Primitive DS
 CREATE

◦ Declare n, a, b as integer

 DESTROY

 SELECTION

 UPDATE

 Na+b
Integer
 Represent the quantity and discrete in nature

 E.g. No of flights arriving at an airport

 {…-(n+1), -n,…,-2,-1,0, 1, 2, …., n, (n+1),..}


Representation of Integer
 Unsigned number – (only positive values)

 Signed numbers
Unsigned Integer
 An unsigned integer is an integer without a sign, that is, a non-negative integer

 They range from zero to infinity, but no computer can store all the integers in that
range

 So, a maximum unsigned integer is defined

 This maximum is based on the number of bits used to store an integer

 Let’s use 8 and 16-bit (1 and 2 bytes) storage locations in our examples

 The length of storage is set by the data type the programmer specifies for a
variable
Unsigned Integer
 An unsigned integer is stored as its value when represented as a binary
number

 Leading zeros are added to fill out the storage location

 For example, the decimal number 9 is represented as 00001001 when


stored in 1-byte because 000010012 = 910

 When stored in a 2-byte location, 9 would be represented as


0000000000001001
Unsigned Integer
 One may use the following table to work with binary numbers:

 For example, given 00001001, what decimal number does it represent?

 Add the non-negative powers of two, that is, 8 + 1 = 9


Unsigned Integer
 One may use the same table to go the other way, that is, given the decimal
number 13, what is its binary representation?

 Find the largest power of 2 that doesn’t exceed the number and place a 1 in
that cell:

 Subtract that power of 2 from the number and use this as the new number:
13 – 8 = 5
Unsigned Integer
 Then continue in this way until the sum of the powers of two equals the
number:

 Now, 5 – 4 = 1, and so finally:

 Note that 8 + 4 + 1 = 13
Unsigned Integer
 Then fill in the remaining cells with zeros:

 So, the unsigned integer representation of decimal 13 is 00001101 when


stored in 1-byte
Unsigned Integer
 If one tries to store a number in a memory location that is not large enough
we have what is called overflow

 In this case, depending on the system, one may or may not receive an error
message

 So, one must not store a number that is larger than the maximum for a
given length of storage

 The maximum number storable in 1-byte is 255


Unsigned Integer
 For example, if one tries to store 256 in 1-byte there is overflow because the
largest value storable in 8 bits is 255 as one can see from the following
table:

 Note that 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255


Signed Integer
 A sign-and-magnitude format is used to allow for positive and negative numbers

(and zero)

 The leading bit is designated as the sign bit: 0 for positive or zero, 1 for negative

 The remaining bits represent the value

 So, in 1-byte of storage the maximum number storable is not 255 as it was for

the unsigned integer representation, but 127:

 Note that 64 + 32 + 16 + 8 + 4 + 2 + 1 = 127


Unsigned Binary Integers
 Given an n-bit number
n1 n2
x  xn12  xn2 2   x12  x0 2
1 0

 Range: 0 to +2n – 1
 Example
 0000 0000 0000 0000 0000 0000 0000 10112
= 0 + … + 1×23 + 0×22 +1×21 +1×20
= 0 + … + 8 + 0 + 2 + 1 = 1110
 Using 32 bits
 0 to +4,294,967,295
Signed Positive Integer
 To determine what the sign-and-magnitude representation of a positive
decimal number is:

◦ Convert the decimal number to binary

◦ If needed add leading zeros to fill the storage location

 For example, decimal 12 is represented in 1-byte as 00001100 because


8 + 4 = 12:
Signed Positive Integer
 Going the other way, given a sign-and-magnitude representation for a
positive number, one can interpret it as follows:

◦ Leftmost bit will be 0 indicating positive

◦ Convert the remaining bits to a decimal number

 For example, 00010001 is decimal 17:

 Because 16 + 1 = 17
Concerns
 Not economical

 Extra Effort required to perform operation (+7+ (-6)) or (-7+ 6)

 Additional operation required to find the sign of the operand.

 Hence, it is proposed to have radix complement representation


2’s complement
 Two’s complement is still a sign-and-magnitude format

 In two’s complement, some of the magnitude bits are flipped from 0


to 1 or 1 to 0

 Modulo M = RN

 R is the radix, N is the number of bits required in the modulo M.

◦ -x = M – x
2’s complement
Integer 2’s Complement Integer 2’s Complement
0 0000 -1 (16-1=15) 1111
1 0001 -2 (16-2=14) 1110
2 0010 -3 (16-3=13) 1101
3 0011 -4 (16-4=12) 1100
4 0100 -5 (16-5=11) 1011
5 0101 -6 (16-6=10) 1010
6 0110 -7 (16-7=9) 1001
7 0111 -8 (16-8=8) 1000

The range of values defined using 4-bit is -8 to 7


2s-Complement Signed Integers
 Given an n-bit number
n1 n2
x  xn12  xn2 2   x12  x0 2
1 0

 Range: –2n – 1 to +2n – 1 – 1

 Example

 1111 1111 1111 1111 1111 1111 1111 11002


= –1×231 + 1×230 + … + 1×22 +0×21 +0×20
= –2,147,483,648 + 2,147,483,644 = –410

 Using 32 bits

 –2,147,483,648 to +2,147,483,647
2s-Complement Signed Integers
 Bit 31 is sign bit
◦ 1 for negative numbers
◦ 0 for non-negative numbers
 –(–2n – 1) can’t be represented
 Non-negative numbers have the same unsigned and 2s-complement representation
 Some specific numbers
◦ 0: 0000 0000 … 0000
◦ –1: 1111 1111 … 1111
◦ Most-negative: 1000 0000 … 0000
◦ Most-positive: 0111 1111 … 1111
Signed Negation
 Complement and add 1

◦ Complement means 1 → 0, 0 → 1
x  x  1111...1112  1

x  1  x

 Example: negate +2

 +2 = 0000 0000 … 00102

 –2 = 1111 1111 … 11012 + 1


= 1111 1111 … 11102
Sign Extension
 Representing a number using more bits
◦ Preserve the numeric value
 In MIPS instruction set
◦ addi: extend immediate value
◦ lb, lh: extend loaded byte/halfword
◦ beq, bne: extend the displacement
 Replicate the sign bit to the left
◦ c.f. unsigned values: extend with 0s
 Examples: 8-bit to 16-bit
◦ +2: 0000 0010 => 0000 0000 0000 0010
◦ –2: 1111 1110 => 1111 1111 1111 1110
2’s complement
 Two’s complement is the standard representation for negative integers in
modern computers

 This is because arithmetic operations are simple to implement when integers


are stored this way

 Although on the surface it seems complicated, at a deeper level it allows for


simplicity of operations
2’s complement
 An alternative but equivalent method for converting a negative number
to its two’s complement representation is:
◦ Ignore the sign and convert the decimal number to binary
◦ If needed add leading zeros to fill the storage location
◦ Flip all the bits
◦ Add 1 to the result of the last step
◦ Make the sign bit 1
 Some people find this easier
Concerns
 Only addition and complement operation sufficient for all operations.

 Adding 7 + 7 or -7 -7 will lead to overflow.


Concerns
 Addition: 5 + 3 = 5 + (+3)

 Subtraction : 5 - 3 = 5 + (-3)
Diminished radix-complement
 Also called as “1’s complement”.

 DRCOMP(X) = Rn – 1 – X

 Rn =Modulo representation

 E.g. X = 6 is represented in modulo 16 bit is 16 – 1 – 6 = 9


Diminished radix-complement
Integer 1’s Complement Integer 1’s Complement
0 0000 -1(16-1-1) 1110
1 0001 -2(16-1-2) 1101
2 0010 -3(16-1-3) 1100
3 0011 -4(16-1-4) 1011
4 0100 -5(16-1-5) 1010
5 0101 -6(16-1-6) 1001
6 0110 -7(16-1-7) 1000
7 0111 -0(16-1-0) 1111

The range of values defined using 4-bit is -7 to 7


Concerns
 Only addition and complement operation sufficient for all operations.

 Available of both -0 and +0 which may lead to computational issues.

 3+4 = 7, 3 -4 = -1, -3 + 4 result in ‘0’.

 7+7 = -1, -7 – 7 = 0

 Error due to overflow.


Overflow rules
 Addition of +ve and –ve number can never cause overflow

 Addition of 2 +ve or 2 –ve numbers does not cause overflow if the


resulting sum has the same sign as the two operands.

 Exception: in 2’s complement adding 2 n bit negative numbers such that


|a| + |b| < 2(n+1)

 Above rules are violated in case of -7 – 7 and +7 +7 in 2’s complement


Integer representation
 Range of values for n-bit storage representation is 2n-1

 In 2’s complement, 2n-1 <= N <= 2n-1 -1

 In 1’s complement, 2n-1 +1 <= N <= 2n-1 -1


Binary coded Decimal (BCD)
 Recent computers and large computers adopt BCD format to
represent integer

 53 in a 4 bit representation is 0101 0011 0

 Mainly used in IBM mainframes

 Last bit is used for negative.

 -53 = 0101 0010 1


Binary coded Decimal (BCD)
 The addition and subtraction of BCD have different rules.

 The BCD arithmetic is little more complicated.

 BCD needs more number of bits than binary to represent the decimal
number. So BCD is less efficient than binary.
Real numbers
 Fixed Point Representation

 In the floating-point notation a real number is represented by a


32-bit string. Including in 32-bit, 24-bit use for representation of
Mantissa and remaining 8-bit use for representation of Exponent
.Both the mantissa and the exponent are twos complement
binary Integers.
Real Numbers
 To assign values ranging from distance of sun to earth
(16,800,000,000,000) and energy emission (0.000000000000832).

 Precision is directly affected in the cost of the computer.

 The general form of a real number in radix R is

◦ F-1 f-2… f-M X RE

 Where F-1 f-2… f-M is the fractional part or mantissa

 And E is the exponent which is always integer


Real Number
 Round off Error - Approximation Vs Accurate measurement

 168 X 1014 vs 168,817,210,391,704 kms

 General Format

Sign Charateristic Fraction


Real Number - Excess notation
 This fixed length notation (i.e., the length of the bit pattern used can
not be altered once set at the beginning) makes it possible to store
negative (-) and non-negative (+ including zero) values by treating the
right-most digits referred to as the Most Significant Bit (MSB) as
representing the sign of the number.

 In excess notation the MSB also known as the sign bit of 1


represents the non-negative (+) sign and a 0 indicates a negative (-)
number.
Real Number - Excess notation
 In the case of a 4-bit pattern, for example: 0110 the digit/column value of the
most significant bit is 8, so 4 bit patterns are referred to as an excess (8)
notation.

 To convert this example find the sum value of the entire pattern as though a
standard binary number:

 (0x8) + (1x4) + (1x2) + (0x1) = 6

 Then subtract the excess value,8, from the sum, (6 - 8)

 The result is a signed value, -2.


Real Number - Excess notation
 In the case of a 5-bit pattern example, 11110, the digit/column value of the most
significant bit is 16, so 5-bit patterns are referred to as an excess (16)•notation.

 To convert this example find the sum value of the entire pattern as though a standard
binary number:

 (1x16) + (1x8) + (1x4) + (1x2) + (0x1) = 16 + 8 + 4 + 2 + 0 = 30

 Then subtract the current excess value, 16, from the sum, (30 - 16). The result is a
signed value, + 14.

 Therefore, it is evident that in excess notation, the sign bit of 0 represents the
negative sign and 1 represents the non-negative sign to denote a signed value.
Real Number - Fractional part or mantissa
 F must lie in the interval R-1 <= F < 1
Data Interpretation in computer
 Character String

◦ For example, in some computers, the eight bits 11000000 is used to

represent the character "A" and 11000001 for character "B" and

another for each character that has a representation in a particular

machine. So, the character string "AB" would be represented by the

bit string 1100000011000001.


Character Representation
 The American Standard Code for Information Interchange (ASCII) is the
scheme used to assign a bit pattern to each of the characters
 ASCII charts come in different flavors:
◦ Some have 7 bit strings, some 8 or more
◦ Some show the binary code for the various characters as well as the
code represented in other number systems, e.g., decimal, hex, octal
 For example, the letter A has the ASCII code:
◦ 1000001 in binary for a 7-bit chart
◦ 65 in decimal Note: All four of these numbers
represent the same value but
◦ 41 in hex using different number
◦ 101 in octal systems
Subset of ASCII Chart
ASCII Chart
 Uppercase characters have different ASCII codes than lowercase

 Uppercase characters come before lowercase

 Numbers come before letters

 The special characters are spread around

 The numbers and upper and lowercase characters are in adjacent


groupings, so that their codes increment by one
ASCII Chart
 The only difference between the binary codes for the upper and
lowercase characters is the sixth bit, that is, the decimal code for a
lowercase character is 32 greater than the uppercase character’s
decimal code

 ASCII codes before decimal 32 are control characters (nonprintable)


like bell, backspace, and carriage return

 The final ASCII code in the 7-bit chart is the control character DEL with
decimal code 127
Extended ASCII & Unicode
 The eight-bit ASCII chart is sometimes called Extended ASCII
 The seven-bit ASCII codes are the same in eight-bit chart except have
a zero at the left
 Some manufactures use the extra bit to create additional special
characters, these however are nonstandard, e.g., using decimal 171
for ½, or 246 for ÷
 Unicode is another scheme developed so that the many symbols in
international languages may be represented. It also uses bit patterns.
UTF-32 uses 32 bits.
EBCDIC
 The extended binary coded decimal interchange code (EBCDIC) is an
8-bit alphanumeric code which has been extensively used by IBM in
its mainframe applications.

 ASCII code is a 7-bit code whereas EBCDIC is an 8-bit code.

 ASCII code is more commonly used worldwide while EBCDIC is used


primarily in large IBM computers.

You might also like