Number System
(Different Ways To Say How Many)
Spring 2017
INTRODUCTION TO INFORMATION AND COMMUNICATION TECHNOLOGIES EMAIL: [email protected]
WEBSITE:
Outline
► Numeral System ► Decimal System ► Binary System ► Octal System ► Hexadecimal System► Digit and Positions
► Bits, Bytes & Words
► Conversions
► Negative Numbers
2/4/2017 Mehwish Fatima- CIIT Lahore
Numeral System
► A Numeral System is a writing-system for expressing numbers, a mathematical notation for representing numbers of a given set, using symbols in a consistent manner.
► Ideally, a numeral system will:
► Represent a useful set of numbers (integers, or rational numbers)
► Give every number represented a unique representation (or a standard representation)
► Reflect the algebraic and arithmetic structure of the numbers.
2/4/2017
Base/Radix of Numeral System
► The number of distinct symbols that can be used to represent numbers in that system.
► For example, the base for the ‘decimal numerical symbol is 10’, as we can use the ten symbols 0,1,2,…,9 to represent numbers in this system.
2/4/2017
4
This means that the same number will have different representations in different systems. So, ‘101’ means a hundred and one in decimal, and five, in binary; while 5 means five in decimal but is invalid in binary as the symbol ‘5’ is not allowed in binary.
Decimal Number System
► The decimal numeral system (base ten or denary) has ten as its base. It is the numerical base most widely used by modern civilizations.
2/4/2017
Binary Number System
► The binary numeral system (base two) has two as its base.
► It is the numerical base used by the modern day computers where numbers need to be stored using the on/off logic of electronic and/or magnetic media. The ON and OFF conveniently translate into 1 and 0.
2/4/2017
Octal Number System
► The octal numeral system (base eight) has eight as its base.
2/4/2017
Hexadecimal Number System
► The hexadecimal numeral system (base 16) has sixteen as its base.
► This system uses the 16 symbols: {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}
► The main advantage of using this numeral system is that it keeps the representations short, and hence manageable.
► For example: the decimal number 199834, is represented in binary as 110000110010011010; and in hexadecimal as 30C9A.
2/4/2017
Why we need all these systems
► The length of representations can grow very rapidly in binary, and even decimal, while slowly in hexadecimal, as extra symbols are allowed.
► So we need hexadecimal to keep things manageable on the paper.
► Computers need binary numbers, but humans can work better with decimal, octal and hexadecimal.
► Hence we should know all these systems and how to convert numbers between these different representations.
2/4/2017
Digit & Position
► The length of a representation grows, from right to left, like: 0,1, 2, 3, … so on.
► Let’s take the example of the decimal system.
► The number nine, in decimal, consists of one digit ‘9’ at position 0.
► Similarly, all the numbers 0, 1, 2…,9 all consist of a single digit at position 0.
► But once we reach 9, we have run out of new symbols (in decimal system) and hence we must increase the length.
► Therefore we add another digit to the left, at position 1. Hence we get 10, 11,12…,19,20,21,…,99.
► when again, we run out of all length-two combinations of symbols and must proceed to length-three representations, starting from 100, so on.
2/4/2017
Digit & Position
► In general, a number x may be represented with a representation of length n in the following manner (here dp means the digit at position p).
► So for the number 199834, in decimal, d0=4, d1=3, d2=8, d3=9, d4=9 and d5=1.
► In general, We call the rightmost digit, d
0, the least significant digit (LSB) and the leftmost digit dn-1,
the most significant digit (MSB).
2/4/2017
BITS, BYTES and WORDS
► A digit in the binary system is more commonly called a bit.
► Therefore, a bit is a single digit in the binary representation of a number and is either 0 or 1.
► When a binary number is represented using 8 bits, the resulting representation, composed of d0, d1, d2… d7, is called a byte.
► Similarly a binary representation composed of 16 bits is called a word, a binary representation composed of 32 bits is called a double word, and a binary representation composed of 64 bits is called a quadruple
word.
2/4/2017
BITS, BYTES and WORDS
► A less common, 4-bit representation of numbers is called nibble.
► The concept of a byte is fundamental in computer science because a byte is the smallest addressable unit of memory in a modern computer; furthermore, data is quantified in terms of byte!
► When you say that your hard-disk can store 20 Giga Bytes, you really mean that it can store 20x230 bytes of data.
► A byte is therefore a unit for measuring data in computers.
2/4/2017
BITS, BYTES and WORDS
► The following quantifiers are important:
► KILO
► 1K = 210 So how many bytes in 37KB?
► MEGA
► 1M = 220 So how many Kilo bytes in 137MB?
► GIGA
► 1G = 230 So how many Tera bytes in 562MB?
► TERA
► 1T = 240 So how Giga bytes in 307TB?
2/4/2017
Representation of a Number
►
2/4/2017
Hold on! Can we use this method to convert a
number from any other base to decimal?
2/4/2017
16
Method-I
►
2/4/2017
Converting numbers from other bases to
their decimal equivalents
► Binary 1001 (so r=2) : ► D = 1x20+0x21+0x22+1x23 = 1+0+0+8 = 9 ► Octal 1767 (so r=8) : ► D = 7x80+6x81+7x82+1x83 = 7+48+448+512 = 1015
► Hexadecimal 12AC (so r=16):
► D = Cx160+Ax161+2x162+1x163 = 12+160+512+4096
= 4780
► (A represents a decimal 10 and C represents a decimal 12)
2/4/2017
This is cool! But how do we solve the converse
problem? How do we convert decimal numbers
into binary, octal and hexadecimal?
2/4/2017 Mehwish Fatima- CIIT Lahore
Decimal to Binary Conversion
►
2/4/2017
Decimal to Binary Conversion
► Now let me repeat this process on the number q
► Can you see what is happening?
► Continuing this process we will reach a point where the quotient cannot be further divided.
2/4/2017
Decimal to Binary Conversion
► And at this point we will have derived the following formula:
► Which is exactly the same as Method-I !!!
► So it turns out that by dividing we were only doing the opposite operation of what we had done before.
► Hence our assumption of taking the successive remainders as d0, d1 and so on was in fact correct!
2/4/2017
Converting Decimal Numbers into their Equivalents in
other Bases
► Generally, if we want to convert a decimal number D into the equivalent number in base r we will simply divide the number D by the base r.
► And then the successive quotients, by r, and pick the successive remainders as digits d0, d1 and so on, of the equivalent number in base r.
► We call this Method-II.
2/4/2017
Conversion from Base r to Base s
► Now we posses enough knowledge to covert a number from any given base to a desired base.
► Simply, we first convert the number into decimal using Method-I, and then, from decimal to the desired base, using Method-II.
► So if you want to convert a number in base r to its equivalent number in base s, you simply follow the following path:
2/4/2017
Direct Conversions
► Sometimes it is much easier to convert numbers between bases directly (without first converting them into decimal).
► You should know these methods for speeding up your calculations.
► Here are some sources for learning these:
► http://www.mindspring.com/~jimvb/binary.htm
► http://atrevida.comprenica.com/atrtut01.html
2/4/2017
We have seen positive numbers representation till now, how would negative numbers will represent in binary system
2/4/2017
Negative Number
► How to represent negative number in binary system
► Signed Magnitude
► 1’s Complement
► 2’s Complement
2/4/2017
Signed Magnitude
► Use the leftmost digit as a sign indication, and treat the remaining bits as if they represented an unsigned integer.
► The convention is that if the leftmost digit (most significant bit) is 0 the number is positive, if it’s 1 the number is negative.
► 00001010 = decimal 10
► 10001010 = decimal -10
2/4/2017
Signed Magnitude
► The main problem with this system is that it doesn’t support binary arithmetic (which is what the computer would naturally do).
► That is, if you add 10 and -10 binary you won’t get 0 as a result.
00001010 (decimal 10) + 10001010 (decimal -10) _____________________ 10010100 (decimal -20) 2/4/2017
29
1’s Complement
► To obtain one’s complement, you simply need to flip all the bits.
► Suppose we are working with unsigned integers.
► Decimal 10 is represented as: 00001010
► It’s one complement would be: 11110101
► Notice that the complement is 245, which is 255 – 10.
► That is no co-incidence.
► The complement of a number is the largest number represented with the number of bits available minus the number itself.
► Since we are using 8 bits here the maximum number represented is 255 (2^32 – 1). So the complement of 10 will be 245.
2/4/2017
1’s Complement
► using the one’s complement system to represent negative numbers we would have two zeroes: 00000000 (could be seen as +0) and 11111111 (could be seen as -0).
00001100 (decimal 12) +11110011 (decimal -12) ____________________ 11111111 (decimal -0) 2/4/2017
31
1’s Complement
► this system partially solves the binary arithmetic problem because there are some special cases left.
► 00000011 (decimal 3)
► +11111101 (decimal -2)
►
---► 100000000 (decimal 256)
► But since we have only 8 bits to represent the numbers, the leftmost 1 will be discarded, and the result would be 00000000 (decimal +0).
► This is not the answer we expected.
2/4/2017
2’s Complement
► Two’s Complement of a binary number is basically another number which, when added to the original, will make all bits become zeroes.
► You find a two’s complement by first finding the one’s complement, and then by adding 1 to it. ► 11110011 (one's complement of 12) ► +00000001 (decimal 1) ► --- ► 11110100 (two's complement of 12) 2/4/2017
33
2’s Complement
► Now let’s add 12 with -5 to see if we’ll have the same problem that we had when using the one’s complement system:
► 00001100 (decimal 12) ► +11111011 (decimal -5) ► ---► 00000111 (decimal 7) 2/4/2017
34
2/4/2017
Number Overflow
► Overflow occurs when the value that we compute cannot fit into the number of bits we have allocated for the result.
► For example, if each value is stored using eight bits, adding 127 to 3 would overflow: ► 01111111 ► + 00000011 ► ---► 10000010 2/4/2017
36
Number Overflow
► 100000010 in our scheme represents 126, not +130. If, however, we were not representing negative numbers, the result would be correct.
2/4/2017
Code for Letters and Symbols
► Computers use a standard binary code to represents letters of the alphabet, numerals, punctuation marks and other special characters.
► The code is called ASCII (pronounced “askey”) which stands for “American Standard Code for Information Interchange.”
► There are 256 code combinations.
2/4/2017