08. Numeral Systems

26
Numeral Systems Numeral Systems Binary, Decimal and Hexadecimal Binary, Decimal and Hexadecimal Numbers Numbers Svetlin Nakov Svetlin Nakov Telerik Telerik Corporation Corporation www.telerik. com

description

Numeral Systems: Positional and Non-Positional Conversions between Positional Numeral Systems: Binary, Decimal and Hexadecimal Representation of Numbers in Computer Memory Exercises: Conversion between Different Numeral Systems

Transcript of 08. Numeral Systems

Page 1: 08. Numeral Systems

Numeral SystemsNumeral SystemsBinary, Decimal and Hexadecimal Binary, Decimal and Hexadecimal

NumbersNumbers

Svetlin NakovSvetlin NakovTelerik Telerik

CorporationCorporationwww.telerik.com

Page 2: 08. Numeral Systems

Table of ContentsTable of Contents

1.1. Numerals SystemsNumerals Systems Binary and Decimal NumbersBinary and Decimal Numbers Hexadecimal NumbersHexadecimal Numbers Conversion between Numeral Conversion between Numeral

SystemsSystems

3.3. Representation of NumbersRepresentation of Numbers Positive and Negative Integer Positive and Negative Integer

NumbersNumbers Floating-Point NumbersFloating-Point Numbers

4.4. Text RepresentationText Representation 2

Page 3: 08. Numeral Systems

Numeral SystemsNumeral SystemsConversion between Numeral Conversion between Numeral SystemsSystems

Page 4: 08. Numeral Systems

Decimal numbers (base Decimal numbers (base 1010)) Represented using Represented using 1010 numerals: numerals: 00, , 11, , 22, , 33, , 44, , 55, , 66, , 77, , 88, , 99

Each position represents a power of Each position represents a power of 1010: : 440011 = = 44*10*1022 + + 00*10*1011 + + 11*10*1000 = = 400400 + + 11 113300 = = 11*10*1022 + + 33*10*1011 + + 00*10*1000 = = 100100 + + 3300 99778866= = 99*10*1033 + + 77*10*1022 + + 88*10*1011 + + 66*10*1000 = =

= = 99*1000 +*1000 + 77*100 + *100 + 88*10 + *10 + 66*1 *1

Decimal NumbersDecimal Numbers

4

Page 5: 08. Numeral Systems

Binary Numeral SystemBinary Numeral System

5

1 0 1 1 0 0 1 01 0 1 1 0 0 1 01 0 0 1 0 0 1 01 0 0 1 0 0 1 01 0 0 1 0 0 1 11 0 0 1 0 0 1 11 1 1 1 1 1 1 11 1 1 1 1 1 1 1

Binary numbers are represented by sequence of bits (smallest unit of information – 0 or 1) Bits are easy to represent in

electronics

Page 6: 08. Numeral Systems

Binary numbers (base Binary numbers (base 22)) Represented by Represented by 22 numerals: numerals: 00 and and 11

Each position represents a power of Each position represents a power of 22: : 110011bb = = 11*2*222 + + 00*2*211 + + 11*2*200 = = 100100bb + + 11bb = = 44 ++ 11 ==

= = 55

111100bb = = 11*2*222 + + 11*2*211 + + 00*2*200 = = 100100bb + + 1010bb = = 44 ++ 22 ==

= = 66 111100110011bb = = 11*2*255 + + 11*2*244 + + 00*2*233 + + 11*2*22 2 + + 00*2*211 + + 11*2*200 === = 3232 + + 1616 + + 44 + + 11 = == = 5353

Binary NumbersBinary Numbers

6

Page 7: 08. Numeral Systems

Binary to Decimal Binary to Decimal ConversionConversion

Multiply each numeral by its exponent:Multiply each numeral by its exponent:

11000011bb == 1 1*2*233 + + 11*2*200 = = 11*8*8 + + 11*1*1==

= = 99

00111111bb = = 00*2*233 + + 11*2*222 + + 11*2*211 + + 11*2*200

=== = 100100bb + + 1010bb + + 11bb = = 44 + + 22 + + 11 = == 7= 7

111100111100bb = = 11*2*255 + + 11*2*244 + + 00*2*233 + + 11*2*22 2 ++ 11*2*211 = = = = 100000100000bb + + 1000010000bb + + 100100bb + + 1010bb = = = = 3232 + + 1616 + + 44 + + 22 = = = 54= 54

7

Page 8: 08. Numeral Systems

Decimal to Binary Decimal to Binary ConversionConversion

Divide by Divide by 22 and append the and append the reminders in reversed order:reminders in reversed order:

500/2 = 250 (0)500/2 = 250 (0)

250/2 = 125 (0)250/2 = 125 (0)

125/2 = 62 (1)125/2 = 62 (1)

62/2 = 31 (0) 62/2 = 31 (0) 500500dd = = 111110100111110100bb

31/2 = 15 (1)31/2 = 15 (1)

15/2 = 7 (1)15/2 = 7 (1)

7/2 = 3 (1)7/2 = 3 (1)

3/2 = 1 (1)3/2 = 1 (1)

1/2 = 0 (1)1/2 = 0 (1)8

Page 9: 08. Numeral Systems

Hexadecimal NumbersHexadecimal Numbers Hexadecimal numbers (base Hexadecimal numbers (base 1616))

Represented using Represented using 1616 numerals: numerals: 00, , 11, , 22, ... , ... 99, , AA, , BB, , CC, , DD, , EE and and FF

Usually prefixed with Usually prefixed with 0x0x0 0 0x0 0x0 8 8 0x8 0x81 1 0x1 0x1 9 9 0x9 0x92 2 0x2 0x2 10 10 0xA 0xA3 3 0x3 0x3 11 11 0xB 0xB4 4 0x4 0x4 12 12 0xC 0xC5 5 0x5 0x5 13 13 0xD 0xD6 6 0x6 0x6 14 14 0xE 0xE7 7 0x7 0x7 15 15 0xF 0xF

9

Page 10: 08. Numeral Systems

Hexadecimal Numbers Hexadecimal Numbers (2)(2)

Each position represents a power of Each position represents a power of 1616: :

99778866hexhex == 99*16*1633 + + 77*16*1622 + + 88*16*1611 + + 66*16*1600 === = 99*4096*4096 + + 77*256*256 + + 88*16*16 + + 66*1*1=== = 3879038790

0x0xAABBCCDDEEFFhexhex == 1010*16*1655 + + 1111*16*1644 + + 1212*16*1633 + + 1313*16*1622 + + 1414*16*1611 + + 1515*16*1600 = === 1125937511259375

10

Page 11: 08. Numeral Systems

Hexadecimal to Hexadecimal to Decimal ConversionDecimal Conversion

Multiply each digit by its exponentMultiply each digit by its exponent 11FF44hexhex == 11*16*1622 + + 1515*16*1611+ + 44*16*1600 = =

= = 11*256*256+ + 1515*16*16 + + 44*1 =*1 =

== 500500dd

FFFFhexhex = = 1515*16*1611 + + 1515*16*1600 === = 240240 + + 1515 === 255= 255dd

11

Page 12: 08. Numeral Systems

Decimal to Hexadecimal Decimal to Hexadecimal ConversionConversion

Divide by Divide by 1616 and append the and append the reminders in reversed orderreminders in reversed order

500/16 = 31 (4) 500/16 = 31 (4)

31/16 = 1 (F)31/16 = 1 (F) 500 500dd = 1F4 = 1F4hexhex

1/16 = 0 (1) 1/16 = 0 (1)

12

Page 13: 08. Numeral Systems

Binary to HexadecimalBinary to Hexadecimal(and Back) Conversion(and Back) Conversion

The conversion from binary to The conversion from binary to hexadecimal (and back) is hexadecimal (and back) is straightforwardstraightforward: : each hex digit each hex digit corresponds to a sequence of corresponds to a sequence of 44 binary digits:binary digits:0x0 = 00000x0 = 0000 0x8 = 10000x8 = 10000x1 = 00010x1 = 0001 0x9 = 10010x9 = 10010x2 = 00100x2 = 0010 0xA = 10100xA = 10100x3 = 00110x3 = 0011 0xB = 10110xB = 10110x4 = 01000x4 = 0100 0xC = 11000xC = 11000x5 = 01010x5 = 0101 0xD = 11010xD = 11010x6 = 01100x6 = 0110 0xE = 11100xE = 11100x7 = 01110x7 = 0111 0xF = 11110xF = 1111 13

Page 14: 08. Numeral Systems

Numbers Numbers RepresentationRepresentation

Positive and Negative Integers Positive and Negative Integers and Floating-Point Numbersand Floating-Point Numbers

Page 15: 08. Numeral Systems

Representation of Representation of IntegersIntegers

A A shortshort is represented by is represented by 1616 bits bits 100100 = 2= 266 + 2 + 255 + 2 + 222 = =

= 00000000 01100100= 00000000 01100100

An An intint is represented by is represented by 3232 bits bits 65545 = 265545 = 21616 + 2 + 233 + 2 + 20 0 ==

= 00000000 00000001 00000000 00001001= 00000000 00000001 00000000 00001001

A A charchar is represented by is represented by 1616 bits bits ‘‘0’0’ = 48 = 2= 48 = 255 + 2 + 244 = =

= 00000000 00110000= 00000000 00110000

15

Page 16: 08. Numeral Systems

Positive and Negative Positive and Negative NumbersNumbers

A number's sign is determined by theA number's sign is determined by theMost Significant BitMost Significant Bit (MSB)(MSB) Only in signed integers: Only in signed integers: sbytesbyte, , shortshort, , intint, , longlong

Leading Leading 00 means positive number means positive number Leading Leading 11 means negative number means negative number

Example: (8 bit numbers)Example: (8 bit numbers)

0XXXXXXX0XXXXXXXbb > 0 > 0 e.g. e.g. 0001001000010010b b = 18= 18

0000000000000000bb = 0= 0

1XXXXXXX1XXXXXXXbb < 0 < 0 e.g. e.g. 1001001010010010b b = -110= -11016

Page 17: 08. Numeral Systems

Positive and Negative Positive and Negative Numbers (2)Numbers (2)

The largest positive 8-bit The largest positive 8-bit sbytesbyte number number is:is:127127 ((2277 -- 11) = ) = 0111111101111111bb

The smallest negative 8-bit number is:The smallest negative 8-bit number is:-128-128 ( (-2-277) = ) = 1000000010000000bb

The largest positive The largest positive 3232-bit -bit intint number number is:is:22 147147 483483 647647 ((223131 -- 11) = ) = 01111…1111101111…11111bb

The smallest negative The smallest negative 3232-bit number is:-bit number is:-2-2 147147 483483 648648 ( (-2-23131) = ) = 10000…0000010000…00000bb

17

Page 18: 08. Numeral Systems

Representation of 8-bit Representation of 8-bit NumbersNumbers

+127+127 = 01111111= 01111111

......

+3+3 = 00000011= 00000011

+2+2 = 00000010= 00000010

+1+1 = 00000001= 00000001

+0+0 = 00000000= 00000000

-1-1 = 11111111= 11111111

-2-2 = 11111110= 11111110

-3-3 = 11111101= 11111101

......

-127-127 = 10000001= 10000001

-128-128 = 10000000= 10000000

Positive 8-bit numbers Positive 8-bit numbers have the format have the format 0XXXXXXX0XXXXXXX Their value is the Their value is the

decimal of their last decimal of their last 77 bits (bits (XXXXXXX)XXXXXXX)

Negative 8-bit numbers Negative 8-bit numbers have the format have the format 1YYYYYYY1YYYYYYY Their value is Their value is 128128 ( (2277) )

minus (minus (--) the decimal ) the decimal of of YYYYYYYYYYYYYY

1001001010010010bb == 2277 –– 1001010010bb === 128= 128 -- 1818 == -110-110

18

Page 19: 08. Numeral Systems

Floating-Point NumbersFloating-Point Numbers Floating-point numbers Floating-point numbers

representation (according to the representation (according to the IEEE IEEE 754754 standard*): standard*):

Example:Example:

19

2k-1 20 2-1 2-2 2-n

S P0 ... Pk-1 M0 M1 ... Mn-1

Sign Exponent Mantissa

1 10000011 01010010100000000000000

Mantissa = 1,322265625Exponent = 4Sign = -1

Bits [22…0]Bits [30…23]Bit 31

* See http://en.wikipedia.org/wiki/Floating_point

Page 20: 08. Numeral Systems

Text Text Representation Representation

in Computer in Computer Systems Systems

Page 21: 08. Numeral Systems

How Computers Represent Text Data?

A text encoding is a system that uses binary numbers (1 and 0) to represent characters Letters, numerals, etc.

In the ASCII encoding each character consists of 8 bits (one byte) of data ASCII is used in nearly all personal

computers In the Unicode encoding each character consists of 16 bits (two bytes) of data Can represent many alphabets

21

Page 22: 08. Numeral Systems

Character Codes – ASCII Table

ExcerpExcerpt from t from

thetheASCII ASCII tabletable

Binary Binary CodeCode

DecimDecimal al

CodeCode

CharactCharacterer

0100000101000001 6565 AA0100001001000010 6666 BB0100001101000011 6767 CC0100010001000100 6868 DD0001000110100011 3535 ##0110110000000000 4848 000011000100110001 4949 110111111001111110 126126 ~~ 22

Page 23: 08. Numeral Systems

Strings of CharactersStrings of Characters

Strings are sequences of characters Null-terminated (like in C)

Represented by arrayRepresented by array

Characters in the strings can be:Characters in the strings can be: 8 bit (ASCII / windows-8 bit (ASCII / windows-12511251 / …) / …) 1616 bit (UTF- bit (UTF-1616))

…… …… …… …… …… …… …… …… \\00

4 4 bytesbyteslengthlength

…… …… …… …… …… ……

23

Page 24: 08. Numeral Systems

QuestionsQuestions??

Numeral SystemsNumeral Systems

http://academy.telerik.com

Page 25: 08. Numeral Systems

ExercisesExercises1.1. Write a program to convert decimal Write a program to convert decimal

numbers to their binary representation.numbers to their binary representation.

2.2. Write a program to convert binary numbers Write a program to convert binary numbers to their decimal representation.to their decimal representation.

3.3. Write a program to convert decimal Write a program to convert decimal numbers to their hexadecimal numbers to their hexadecimal representation.representation.

4.4. Write a program to convert hexadecimal Write a program to convert hexadecimal numbers to their decimal representation.numbers to their decimal representation.

5.5. Write a program to convert hexadecimal Write a program to convert hexadecimal numbers to binary numbers (directly).numbers to binary numbers (directly).

6.6. Write a program to convert binary numbers Write a program to convert binary numbers to hexadecimal numbers (directly).to hexadecimal numbers (directly).

25

Page 26: 08. Numeral Systems

Exercises (2)Exercises (2)7.7. Write a program to convert from any Write a program to convert from any

numeral system of given base numeral system of given base ss to any to any other numeral system of base other numeral system of base dd ( (2 2 ≤ ≤ ss, , dd ≤ ≤ 1616).).

8.8. Write a program that shows the binary Write a program that shows the binary representation of given representation of given 1616-bit signed -bit signed integer number (the C# type integer number (the C# type shortshort).).

9.9. * Write a program that shows the internal * Write a program that shows the internal binary representation of given binary representation of given 3232-bit -bit signed floating-point number in IEEE signed floating-point number in IEEE 754754 format (the C# type format (the C# type floatfloat). Example: ). Example: --27,2527,25 sign = sign = 11, exponent = , exponent = 1000001110000011, , mantissa = mantissa = 1011010000000000000000010110100000000000000000..

26