Number Systems

download Number  Systems

If you can't read please download the document

description

Number Systems. Character Representation. ASCII American Standard Code for Information Interchange Standard encoding scheme used to represent characters in binary format on computers 7-bit encoding, so 128 characters can be represented - PowerPoint PPT Presentation

Transcript of Number Systems

Slide 1

Number Systems

ASCIIAmerican Standard Code for Information Interchange Standard encoding scheme used to represent characters in binary format on computers 7-bit encoding, so 128 characters can be represented 0 to 31 (& 127) are "control characters" (cannot print)Character Representation

Decimal, hex & character representations are easier for humans to understand; howeverAll the data in the computer is binaryAn int is typically 32 binary digitsint y = 5; (y = 0x00000005;)In computer y = 00000000 00000000 00000000 00000101A char is typically 8 binary digitschar x = 5; (or char x = 0x05;)In computer, x = 00000101

Binary Data

Computer systems are constructed of digital electronics. That means that their electronic circuits can exist in only one of two states: on or off. Most computer electronics use voltage levels to indicate their present state. For example, a transistor with five volts would be considered "on", while a transistor with no voltage would be considered "off.These patterns of "on" and "off" stored inside the computer are used to encode numbers using the binary number system. Because of their digital nature, a computer's electronics can easily manipulate numbers stored in binary by treating 1 as "on" and 0 as "off.How Computers Store Numbers

A number system defines how a number can be represented using distinct symbols. A number can be represented differently in different systems. For example, the two numbers (2A)16 and (52)8 both refer to the same quantity, (42)10, but their representations are different.Number SystemS = {0, 1, 2, 3, 4, 5, 6, 7}

Common Number SystemsSystemBaseSymbolsUsed by humans?Used in computers?Decimal100, 1, 9YesNoBinary20, 1NoYesOctal80, 1, 7NoNoHexa-decimal160, 1, 9,A, B, FNoNo

The word decimal is derived from the Latin root decem (ten). In this system the base b = 10 and we use ten symbols:

The symbols in this system are often referred to as decimal digits or just digits.

DecimalS = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

The word binary is derived from the Latin root bini (or two by two). In this system the base b = 2 and we use only two symbols:

The symbols in this system are often referred to as binary digits or bits (binary digit).

BinaryS = {0, 1}

The word hexadecimal is derived from the Greek root hex (six) and the Latin root decem (ten). In this system the base b = 16 and we use sixteen symbols to represent a number. The set of symbols is:

Note that the symbols A, B, C, D, E, F are equivalent to 10, 11, 12, 13, 14, and 15 respectively. The symbols in this system are often referred to as hexadecimal digits.

Each hexadecimal digit represents four binary digits (bits), and the primary use of hexadecimal notation is a human-friendly representation of binary-coded values in computing and digital electronics.

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

The word octal is derived from the Latin root octo (eight). In this system the base b = 8 and we use eight symbols to represent a number. The set of symbols is:

Each octal digit represents three binary digits (bits)

OctalS = {0, 1, 2, 3, 4, 5, 6, 7} S = {0, 1, 2, 3, 4, 5, 6, 7}

Types of Number System

12

Conversion Among Bases

Decimal

Octal

BinaryHexadecimalThe possibilities:

Quick Example2510 = 110012 = 318 = 1916Base

Binary to Decimal

Decimal

Octal

BinaryHexadecimal

TechniqueMultiply each bit by 2n, where n is the power of the bitThe power is the position of the bit, starting from 0 on the rightADD the resultsBinary to Decimal1010112 => 1 x 20 = 11 x 21 = 20 x 22 = 01 x 23 = 80 x 24 = 01 x 25 = 324310ExampleBit 0

TechniqueMultiply each bit by 2n, where n is the power of the bitThe power is the position of the bit, starting from 0 on the rightADD the resultsFRACTIONSBinary to Decimal10.01102 => 1 0 . 0 1 1 0Example2-42-32-2 2-12021Bit 00 x(1/16) = 0 1 x(1/8) = 1/81 x(1/4) = 1/40 x(1/2) = 00 x(1) = 01 x(2) = 2

2-42-32-22-12021

Ans: 2.375

Octal to Decimal

Decimal

Octal

BinaryHexadecimal

TechniqueMultiply each bit by 8n, where n is the power of the bitThe power is the position of the bit, starting from 0 on the rightAdd the resultsOctal to DecimalExampleBit 07248 => 4 x 80 = 4 2 x 81 = 16 7 x 82 = 448 46810

Hexadecimal to Decimal

Decimal

Octal

BinaryHexadecimal

TechniqueMultiply each bit by 16n, where n is the power of the bitThe power is the position of the bit, starting from 0 on the rightAdd the resultsHexadecimal to DecimalExampleBit 0ABC16 =>C x 160 = 12 x 1 = 12B x 161 = 11 x 16 = 176A x 162 = 10 x 256 = 2560 274810

Decimal to Binary

Decimal

Octal

BinaryHexadecimal

TechniqueDivide by the base 2, keep track of the remainderKeep dividing until the quotient is 0.Take the remainder from the bottom and move upwards as the answerDecimal to BinaryE.g.: Convert 12510 to binary

Take the remainder from bottom upwards as answer

Ans:11111012

TechniqueFor the numbers after the point, multiply it by 2From the answer, take again the fraction part and multiply it by 2 againKeep on multiplying the fraction by 2 until the fraction part is 0FRACTIONSDecimal to BinaryE.g.: Convert 12.2510 to binary

Ans:1100.01212 / 2 = 6 6 / 2 = 3 3 / 2 = 1 1 / 2 = 0 R0R0R1R1 0.25 X 2 = 0.50

0.50 X 2 = 1.00 12 . 25011100

3.14579 .14579x 20.29158x 20.58316x 21.16632x 20.33264x 20.66528x 21.33056etc.11.001001...FRACTIONSDecimal to Binary

Octal to Binary

Decimal

Octal

BinaryHexadecimal

TechniqueConvert each octal digit to a 3-bit equivalent binary representation

1 octal digit = 3 binary digitsOctal to BinaryE.g.: Convert 7058 to binary 7 0 5

111 000 101Start from the right 0 bit4 2 14 2 14 2 1Ans: 111 000 1012

Binary to Octal

Decimal

Octal

BinaryHexadecimal

TechniqueDivide the binary bits in group of 3s, starting from the RIGHTAdd 0s to the last group to make it 3 bitsConvert each grouped binary to their octal digitsBinary to OctalE.g.: 10110101112001 011 010 111

1 3 2 7Start from the right 0 bit4 2 1Ans: 132784 2 14 2 14 2 1001 011 010 111Divide the binary numbers into groups of 3s. Add 0 to the last group to make it 3 bits

Hexadecimal to Binary

Decimal

Octal

BinaryHexadecimal

TechniqueConvert each hexadecimal digit to a 4-bit equivalent binary representation

1 hexadecimal digit = 4 binary digitsHexadecimal to BinaryE.g.: Convert 3A816 to binary 3 A (10) 5

0011 1010 0101Start from the right 0 bit8 4 2 1Ans: 0011 1010 010128 4 2 18 4 2 1

Binary to Hexadecimal

Decimal

Octal

BinaryHexadecimal

TechniqueDivide the binary bits in group of 4s, starting from the RIGHTAdd 0s to the last group to make it 4 bitsConvert each grouped binary to their hexadecimal digitsBinary to HexadecimalE.g.: 101011101120010 1011 1011

2 11 (B) 11 (B)Start from the right 0 bit8 4 2 1Ans: 2BB168 4 2 18 4 2 10010 1011 1011Divide the binary numbers into groups of 4s. Add 0 to the last group to make it 4 bits

Decimal to Octal

Decimal

Octal

BinaryHexadecimal

TechniqueDivide by the base 8, keep track of the remainderKeep dividing until the quotient is 0.Take the remainder from the bottom and move upwards as the answerDecimal to OctalE.g.: Convert 123410 to octal

Ans:23228

Take the remainder from bottom upwards as answer

Decimal to Hexadecimal

Decimal

Octal

BinaryHexadecimal

TechniqueDivide by the base 16, keep track of the remainderKeep dividing until the quotient is 0.Take the remainder from the bottom and move upwards as the answerDecimal to HexadecimalE.g.: Convert 207910 to binary

Ans:81F16

Take the remainder from bottom upwards as answer

Octal to Hexadecimal

Decimal

Octal

BinaryHexadecimal

TechniqueFirst step: Convert the octal digits to their 3-bits binarySecond step: Combined the binary obtainedThird step: Divide the binary into groups of 4 (hexadecimal) starting from the RIGHTFourth step: Find the hexadecimal digit from the grouped binary

Octal to HexadecimalE.g.: 107681 0 7 6

Start from the right 0 bit8 4 2 1Ans: 23E16Divide the binary numbers into groups of 4s. Add 0 to the last group to make it 4 bits0010001111100010 / 0011 / 11108 4 2 18 4 2 114 (E)32

Hexadecimal to Octal

Decimal

Octal

BinaryHexadecimal

TechniqueFirst step: Convert the hexadecimal digits to their 4-bits binarySecond step: Combined the binary obtainedThird step: Divide the binary into groups of 3 (octal ) starting from the RIGHTFourth step: Find the octal digit from the grouped binary

Hexadecimal to OctalE.g.: C4516C (12) 4 5

Start from the right 0 bit4 2 1Ans: 61058Divide the binary numbers into groups of 4s. Add 0 to the last group to make it 4 bits110001000101110 / 0 01 / 00 0 / 1014 2 14 2 10164 2 15

Binary Addition Binary addition between 2 1-bit values:ABA + BZ= A+B000+00010+11101+01111+110TWO

10101 + 11001 10111011

21 + 25 46

Two n-bit valuesAdd individual bitsPropagate carriesE.g.:Binary Addition

ABA - BZ = A-B000-00010-11, borrow 1101-01111-10Binary Subtraction Binary subtraction between 2 1-bit values:

1011 - 101 1101

Binary Subtraction 10111 - 1001 - 1110E.g.: 1001 10111 = ? 10111 is larger than 1001. So swap so that the larger number is in front and the smaller number is after.Subtract the numbers and negate the answer by putting the - sign in front.Borrow 1 from the next binary digit. Hence the number will become 10 which is equals to 2. In situations like A B, whereby the binary number B is larger than A, swap the number so that B is in front: B A. Then negate the answer: B A = -Y

SIGN BINARYRepresentation of positive and negative numbers in a computer storage

If the SIGN bit is 1, the number is NEGATIVEWhat happens if we make the entire byte all 1s?

11111111= -127

For number -15, the 8-bits binary is 10001111

One way to represent positive or negative numbers would be to make the left most bit a SIGN bit.

Example: 00001111

By using that bit for sign, now the maximum positive number we can represent is for 7 bits only:

i.e.: 01111111 = +127 instead of 255 before.

SIGN bitOnly 7-bits taken for calculationIf the SIGN bit is 0, the number is POSITIVEFor number +15, the 8-bits binary is 00001111

1s complementThe 1's complement of a number is found by changing all 1's to 0's and all 0's to 1's.

This is called as taking complement or 1's complementExample of 1's Complement is as follows:

2s complementThe 2's complement of binary number is obtained by adding 1 to the Least Significant Bit (LSB) of 1's complement of the number.

2's complement = 1's complement + 1Example of 2's Complement is as follows.

SIGN BINARY (ADDITION & SUBTRACTION)

E.g.1 gives + answer due to the positive number is larger than the negative.

E.g.2 gives - answer due to the negative number is larger than the positive.

There are two situations of sign binary subtraction/addition:

Negative number larger = - ANSPositive number larger = + ANS

Sign binaries can be added or subtracted.

E.g.: -120 + 200 is also equal to 200 - 120

E.g.1: 200 120 = +80

E.g.2: 120 200 = -80

Both situations give different result. Subtracting/Adding binary numbers are done by first taking the NEGATIVE value and apply what is known as:1s complement: convert 1 to 0 and 0 to 12s complement: adding 1 to the 1s complement.

SIGN BINARY (ADDITION & SUBTRACTION)

E.g.1:23510 10210 = 133First find the 8-bits binary of each decimal.11101011 01100110= 235= 10210011001+ 1 1s comp10011010+1110101110000101= 133 = -102= 235 2s comp

E.g.2:10010 6010 = 40First find the 8-bits binary of each decimal.0110010000111100= 100= 6011000011+ 111000100+0110010000101000= 40 = -60= 100 1s comp 2s comp11Ignore the carry bcos there should only be 8-bits Ignore the carry bcos there should only be 8-bits

SIGN BINARY (ADDITION & SUBTRACTION)

Add this value to the smaller number.

Apply 1s & 2s complement to the value (leave the sign bit as 1 when performing the 1s comp) to get the final result. E.g.: 8-bits binary 10001101

E.g.: 16-bits binary 0000110100001101

NEGATIVE NUMBERS

The previous example is subtracting a smaller number from a larger number. If you want to subtract a larger number from a smaller number (giving a negative result), then the process is slightly different.

Here are the steps for subtracting a large number from a smaller one (negative result):

Apply 1s & 2s complement to the larger number. Usually, to indicate a negative most significant bit (left hand bit) number, the is set to 1 and the remaining digits are used to express the value.

In this format the MSB is referred to as the sign bit. Sign bit7 bits to express valueSign bit15 bits to express value

SIGN BINARY (ADDITION & SUBTRACTION)

E.g.1:10010 12010 = -20First find the 8-bits binary of each decimal.01100100 01111000= 100= 12010000111+ 1 1s comp10001000+0110010011101100 = -120= 100 2s comp

E.g.2:9610 16010 = -64First find the 8-bits binary of each decimal.1010000001100000= 160= 9601011111+ 101100000+0110000011000000 = -160= 96 1s comp 2s compLeave the sign bit as it is. Do not 1s complement it. 0010011+ 110010100 1s comp 2s comp= -20 0111111+ 111000000 2s comp 1s comp= -64Leave the sign bit as it is. Do not 1s complement it.Arithmetic Overflow

What is overflow?

Overflow or arithmetic overflow is a condition that occurs when a calculation produces a result that is greater in magnitude than what a given data type can store or represent.How to identify the condition of occurrence of overflow?

Assuming were dealing with an 8-bit computer, lets look at the situations below:

E.g.1: 65 + 65 = +130 (Overflow!)

E.g.2: +128 5 = +123 (Overflow!)

An overflow occurs when the INPUT or OUTPUT exceeds the range of the whole number of the bits (which in this case is 8-bits) contained which is between -128 to +127.

We can check the range of whole numbers for unsign and sign binary using the next formulas.Range of Whole Numbers for a computerSIGN & UNSIGN BINARY

For UNSIGNED numbers, to find the range of numbers, used the following formula:

where bits b = 2b - 1

so for an 8-bits binary, the range is 0 to 255: 2 - 1 = 255For SIGNED numbers, , to find the range of numbers, used the following formula:

so for an 8-bits binary, the range is between -128 to 127 - (28-1) to 28-1 1= - 27 to 27 1= - 128 to 1270 to b = 2b 1- (2b-1) to 2b-1 1Floating Point Number

Floating point describes a system for representing numbers that would be too large or too small to be represented as integers.

Numbers are in general represented approximately to a fixed number of significant digits and scaled using an exponent. xFloating Point Number consists of sign, mantissa and exponent.

The SignThe sign of a binary floating-point number is represented by a single bit. A 1 bit indicates a negative number, and a 0 bit indicates a positive number.

How do floating-numbers store?The MantissaThe mantissa, also known as the significand, represents the precision bits of the number. The mantissa of a floating-point number is expressed as a binary number.

Floating Point Number

There are two most common floating point storage format:The ExponentIEEE Short Real exponents are stored as 8-bit unsigned integers with a bias of 127.

While the exponent can be positive or negative, in binary formats it is stored as an unsigned number that has a fixed "bias" added to it.The exponent bias for single precision is 127 and for double precision is 1023.Single PrecisionIEEE Short Real: 32 bits

Sign: 1 bit Exponent: 8 bitsMantissa: 23 bits

Double PrecisionIEEE Long Real: 64 bits

Sign: 1 bit Exponent: 11 bitsMantissa: 52 bits

To calculate the floating point numbers for known width of mantissa and exponent, we use this formula:Value = (-1)sign x (1.mantissa) x 2exp-127

Floating Point Number

Represent the following binary number into 32-bit IEEE Single Precision floating point number: E.g.1: 1.1101 x 220

s = 0, since the sign is positive m = 11010000000000000000000e = 10010011WORKING in finding the exponent:

e = 2020 = e 127e = 20 + 127e = 147

147 change to binary (8-bits for single precision) is 10010011.

Hence, e = 10010011ANS: 0 10010011 11010000000000000000000

Floating Point Number

Represent the following 32-bit IEEE Single Precision floating point number into binary number: E.g.2: 1 00001100 10001110000000000000000

s= 1e= 00001100 = 12m= 1000111

Value = (-1)s x (1.m) x 2e-127 = (-1)1 x (1.1000111) x 212-127 = -1.1000111 x 2115ANS: -1.1000111 x 2115