Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Number system part 1

6,596 views

Published on

Number system part 1

Published in: Education, Technology, Business
  • Great article. Thanks for the info, very helpful. BTW, if anyone needs to fill out a “2009 LANG 335-8, [20091001]”, I found a blank form here: "www.la.ng.mil" and also here "form 335 8"
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Number system part 1

  1. 1. NUMBER SYSTEM Part 1
  2. 2. Objectives - To understand 3.1 Analyses how numeric data are represented in computers I. II. III. IV. V. Decimal representation of numbers (Signed and Unsigned) Integers Fixed Point and Floating-Point numbers Number systems used in computing Binary, Octal, Hexa-decimal logic operation Conversions among number systems
  3. 3. Introduction
  4. 4. Introductio n A number system defines a set of values used to represent a quantity. The study of numbers is not only related to computers. We apply numbers everyday, and knowing how numbers work, will give us an insight of how computers manipulate and store numbers. A number is a mathematical object used in counting and measuring. It is used in counting and measuring. Numerals are often used for labels, for ordering serial numbers, and for codes like ISBNs. In mathematics, the definition of number has been extended over the years to include such numbers as zero, Positive numbers , Negative numbers , Integers numbers , Rational numbers, Irrational numbers, Whole number and complex numbers. Certain procedures which take one or more numbers as input and produce a number as output are called numerical operation.
  5. 5. Types of numbers Complex number:- The complex numbers consist of all numbers of the form (a+bi) ; Where a and b are real numbers and i=0.1 Ex :- 2+5i , 3+3i , … . .. . . . , 14+20i Real Numbers (R) :- The real numbers include all of the measuring numbers Ex :- -69 , -2 , -1.5 , +5 …+ 7.5 . .. . . . , +14 Rational Numbers (Q) :- A rational number is a number that can be expressed as a fraction with an integer and a non-zero natural number denominator Ex :- -25 , -15/10 , 0 , 0.05 , 0.2 , 7/3 , 25 ,36 Irrational Numbers:- A decimal that can be written as a fraction either ends (terminates)or forever repeats about which we will see in detail further. Ex – π =3.14159365358979 Natural numbers (N) :- All positive numbers (Counting numbers) started with 1.However in the 19th century, mathematicians started including 0 in the set of natural numbers. Ex :- 0,1,2,3,4,……,55 ,105 . .. . . .
  6. 6. Types of numbers Integers (Z):- Integers are the number which includes positive and negative numbers. Negative numbers are usually written with a negative sign (also called a minus sign)in front of the number they are opposite of .When the set of negative numbers is combined with the natural numbers zero, the result is the set of integer numbers Ex :- -25 , -15 , +0 , +25 , +36 Fractions :- This is a type of a rational number. Fractions are written as two numbers (m/n), the numerator (m) and the denominator(n) 1. Fixed point Number 2.Floating point number
  7. 7. Fixed point number :- Involving or being a mathematical notation(as in a decimal system) in which the point separating whole numbers and fractions is fixed A fixed point number has a specific number of bits (or digits) reserved for the integer part (the part to the left of the decimal point) and a specific number of bits reserved for the fractional part (the part tothe right of the decimal point). No matter how large or small your number is, it will always use the same number of bits for each portion. For example:- if your fixed point format was in decimal IIIII.FFFFF then the largest number you could represent would be 99999.99999 and the smallest would be 00000.00001. Every bit of code that processes such numbers has to have built-in knowledge of where the decimal point is. 123.56 25.23 8955.3 0.5 0.0098 A fixed point number just means that there are a fixed number of digits after the decimal point.
  8. 8. Floating point number :A floating point number does not reserve a specific number of bits for the integer part or the fractional part. Instead it reserves a certain number of bits for the number (called the mantissa or significant ) and a certain number of bits to say where within that number the decimal place sits (called the exponent). For example:-So a floating point number that took up 10 digits with 2 digits reserved for the exponent might represent a largest value of 9.9999999e+50 and a smallest value of 0.0000001e-49. 1.2 × 10 2 35.02 × 10 80 0.258 × 10 -14 / 2.58 × 10 -15 / 25.8 × 10 -16 A floating point number allows for a varying number of digits after the decimal point.
  9. 9. Types of numbers
  10. 10. Precision({Í ý<) and Accuracy (ùÚØ<÷À]õ$< •Precision is the measure of how closely individual measurements agree with one another. • Accuracy is how closely individual measurements with the correct value.
  11. 11. Significant Digits( µ<µ&&Ú &‘ç]$‘æ×) "Significant Digits", also called "Significant Digits of Precision ", are the digits in a number starting with the first non-zero digit and ending with the last non-zero digit. Those digits can be anywhere relative to the decimal. Non-zero digits are always significant. 22 has two significant digits, 22.3 has three significant digits. With zeroes, the situation is more complicated a. Zeroes placed before other digits are not significant; 0.046 has two significant digits. b. Zeroes placed between other digits are always significant; 4009 kg has four significant digits. c. Zeroes placed after other digits but behind a decimal point are significant; 7.90 has three significant digits. 85.00 g has four significant digits 9.000 000 000 mm has 10 significant digits
  12. 12. Significant Digits d. Zeros at the end of a number but to the left of a decimal may or may not be significant. If such a zero has been measured, or is the first estimated digit, it is significant. On the other hand, if the zero has not been measured or estimated but is just a placeholder, it is not significant. A decimal placed after the zeros indicates that they are significant 1. 2000 m may contain from one to fou r significant digits, depending on how many zeros are placeholders. For measurements given in this text , assume that 2000 has one significant digits. 2. 2000 . m contains four significant digits, indicated by the presence of the decimal point 3.1000.0 has five significant digits (the ".0" tells us something interesting about the presumed accuracy of the measurement being made: that the measurement is accurate to the tenths place, but that there happen to be zero tenths) For example:- in the number 8200, it is not clear if the zeroes are significant or not. The number of significant digits in 8200 is at least two, but could be three or four. To avoid uncertainty, use scientific notation to place significant zeroes behind a decimal point: i.8.200 *103 has four significant digits ii.8.20 *103 has three significant digits iii.8.2 *103 has two significant digits
  13. 13. Significant Digits Scientific notation - All digits expressed before the exponential term are significant. 5.060 x 10-3 m has four sig figs. 9.00 x 102 g has three sig figs.
  14. 14. Most Significant Digit Significant Digit •The MSD in a number is the digit that has the greatest effect on that number. Highest power of base weighting The digits on the left hand side are called the high-order digits (higher powers of 10) Least Significant Digit (MSD and LSD) • The LSD in a number is the digit that has the least effect on that number. • Lowest power of base weighting • Digits on the right hand side are called the low-order digits (lower powers of 10).
  15. 15. Significant Digit You can easily see that a change in the MSD will increase or decrease the value of the number the greatest amount. Changes in the LSD will have the smallest effect on the value. The nonzero digit of a number that is the farthest LEFT is the MSD, and the nonzero digit farthest RIGHT is the LSD, as in the following example
  16. 16. Key principle of numbering systems Radix point (´ÕÙæ ÙæßÂ]× ) Divides fractional portion from the whole portion of a number Weighting Factor ( ýØ &$øæ× ) A multiplier value is used in each column position of a number . It represents the weight factor. Its value determines how many times the Base value is multiplied by itself thus giving the placeholders seen below from right to left labelled as "Ones", "Tens", "Hundreds", "Thousands", "Ten Thousands" and so on. . .
  17. 17. OSITIONAL (&ßö$ùÛ×)NUMBER SYSTEMS In a positional number system, the position a symbol occupies in the number determines the value it represents. In this system, a number represented as: In our decimal number system, the value of a digit depends on its place, or position, in the number. Each place has a value of 10 times the place to its right. A number in standard form is separated into groups of three digits using commas. Each of these groups is called a period.
  18. 18. OSITIONAL (&ßö$ùÛ×)NUMBER SYSTEMS
  19. 19. OSITIONAL (&ßö$ùÛ×)NUMBER SYSTEMS
  20. 20. OSITIONAL (&ßö$ùÛ×)NUMBER SYSTEMS In a positional number system, the position a symbol occupies in the number determines the value it represents. In this system, a number represented as: has the value of: in which S is the set of symbols, b is the base (or radix).
  21. 21. Key principle of numbering systems Positional value significance Gives weight each digit contributes to the number’s overall value Ex:-1.Steps for base 10 Determine positional value of each digit by raising 10 to position within number 2. Steps for base 2 Determine positional value of each digit by raising 2 to position within number
  22. 22. Calculate Position Value and Weighed General procedure (any base)  Calculate position value of the number by raising the base value to the power of the position  Multiply positional value by digit in that position  Add each calculated value together
  23. 23. Number Representations Integer numbers are those numbers which do not have fractional parts. Integer numbers include both positive numbers and negative numbers. They can be handled using any of the following representations Unsigned notation Signed magnitude notion 
  24. 24. Unsigned notation An unsigned integer is an integer that can never be negative and can take only 0 or positive values. Its range is between 0 and positive infinity. All unsigned numbers are Positive 0 , 1 , 2 , 3 , 4 , . . . . , 1598 , . . . . Range = 0 to (2n – 1) ; n is the number of bits used to store the unsigned integer. Numbers with values GREATER than (2n – 1) would require more bits. If you try to store too large a value without using more bits, OVERFLOW will occur. Example: On a system that stores unsigned integers in 16-bit words: Range = 0 to (216 – 1) = 0 to 65535 Therefore, you cannot store numbers larger than 65535 in 16 bits.
  25. 25. Unsigned representation Advantages: 1.One representation of zero 2.Simple addition Disadvantages : 1.Negative numbers can not be represented. 2.The need of different notation to represent negative numbers.
  26. 26. Signed notation  Until now we've been concentrating on unsigned numbers. In real life we also need to be able represent signed numbers (like : -12, -45, +78).  A signed number MUST have a sign (+/-). A method is needed to represent the sign as part of the binary representation. All signed numbers are negative and positive -1.5× 103 , - 25 , -9.6×10-4 , +0 , +5.7×10-4 , +0.5 , +98 , +4.6 ×103 Range =-(2(n-1) – 1) to +(2(n-1) – 1) ; n is the number of bits used to store the sign/magnitude integer Numbers with values GREATER than +(2(n-1) – 1) and values LESS than -(2(n-1) – 1) would require more bits. If you try to store too large/too small a value without using more bits, OVERFLOW will occur. Example: On a system that stores unsigned integers in 16-bit words: Range = -(2(16-1) – 1) to +(2(16-1) – 1) = -(2(15) – 1) to +(2(15) – 1) = -32767 to +32767 Therefore, you cannot store numbers larger than 32767 or smaller than -32767 in 16 bits.
  27. 27. Sign Representation Advantages: Represents positive and negative numbers Disadvantage :  Arithmetic operations are difficult. Addition and subtractions are difficult. Signs and magnitude, both have to carry out the required operation. They are two representations of 0 (Binary number system) 00000000 = + 010 10000000 = - 010 To test if a number is 0 or not, the CPU will need to see whether it is 00000000 or 10000000. 0 is always performed in programs. Therefore, having two representations of 0 is inconvenient.
  28. 28. Summary Binary Unsigned No. of bits Sign-magnitude Min Max Min Max 1 0 1 2 0 3 -1 1 3 0 7 -3 3 4 0 15 -7 7 5 0 31 -15 15 6 0 63 -31 31 Etc. No. of bits n Binary Unsigned Sign-magnitude Min Max 0 2 -1 n Min n-1 -(2 – 1) Max n-1 2 -1
  29. 29. Numeric Representation s
  30. 30. Numbering Systems  Integer ……………. Z Decimal…………… Base 10 (N10 )  Binary …………….. Base 2 (N2 )  Octal ………………. Base 8 (N8 )  Hexadecimal ……. Base 16 
  31. 31. Integer
  32. 32. Integer A number with no fractional part Includes the counting numbers {1, 2, 3, ...}, zero {0}, and the negative of the counting numbers {-1, -2, -3, ...} You can write them down like this: {..., -3, -2, -1, 0, 1, 2, 3, ...} Examples of integers: -16, -3, 0, 1, 198
  33. 33. Decimal
  34. 34. Decimal Number System   The decimal system is a base-10 system. There are 10 distinct digits (0 to 9) to represent any quantity. 10 digits { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }   For an n-digit number, the value that each digit represents depends on its weight or position. Digit Position Integer & fraction 123.456  The weights are based on powers of 10. Weight = (Base) Position 1024 = 1*103 + 0*102 + 2*101 + 4*100 = 1000 + 20 + 4 Positional value example: 436.95 To the left of the radix point : 4 in position two / 3 in position one / 6 in position zero To the right of the radix point: 9 in position negative one / 5 in position negative two
  35. 35. Decimal Number System  Base (also called radix) = 10   Digit Position   Integer & fraction Weight = (Base) 0 -1 -2 7 4 10 1 0.1 0.01 10 2 0.7 0.04 Position Sum of “Digit x Weight” Formal Notation 1 5 1 2 100 Magnitude   2 Digit Weight   10 digits { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } 500 d2*B2+d1*B1+d0*B0+d-1*B-1+d-2*B-2 (512.74)10
  36. 36. Example For D = 52.946
  37. 37. Standard decimal representation Uses the ten numbers from 0 to 9 Each column represents a power of 10 Fixed-point representation Uses the two numbers from 0 to 9 Every column represents a power of 10
  38. 38. Positional Value (cont’d.) Positional values for a base 10 number
  39. 39. Whole Numbers (Radix = 10): 123410 = 1 × 103 + 2 × 102 + 3 × 101 + 4 × 100 With Fractional Part (Radix = 10): 36.7210 = 3 × 101 + 6 × 100 + 7 × 10-1 + 2 × 10-2 General Case (Radix = R): (S1S0.S-1S-2)R = S1 × R1 + S0 × R0 + S-1 × R -1 + S-2 × R-2 Reals
  40. 40. Practice (base 10) 1) 258 = 2.58 x 10 2 Mantissa = 258 Radix = 10 Exponent = 2 3) One billion =1,000,000,000 =1 x 109    significand or mantissa: 1 base or radix: 10 exponent: 9 (Scientific notation ) 4) 1999 =1.999 x 103 significand or mantissa: 1999 base or radix: 10 exponent: 3 =19.99 x 102 =199.9 x 101 2) 24.25 = 2.425 x 10 1 =Mantissa = 2425 =Radix = 10 =Exponent = 1 2) The following shows the place values for the integer +224 in the decimal system. Note that the digit 2 in position 1 has the value 20, but the same digit in position 2 has the value 200. Also note that we normally drop the plus sign, but it is implicit.
  41. 41. Example The following shows the place values for the decimal number −7508. We have used 1, 10, 100, and 1000 instead of powers of 10. ( ) Values Note that the digit 2 in position 1 has the value 20, but the same digit in position 2 has the value 200. Also note that we normally drop the plus sign, but it is implicit. Example The following shows the place values for the real number +24.13.
  42. 42. Binary
  43. 43. Binary Number Systems  The binary system is a base-2 system.  There are 2 distinct digits (0 and 1) to represent any quantity. 2 digits { 0, 1 }, called binary digits or “bits” • 0 = represents no value • 1 = represents a unit value  For an n-digit number, the value of a digit in each column depends on its position.  The weights are based on powers of 2. Weight = (Base) Position 1011 2 = 1*2 3 + 0*2 2 + 1*2 1 + 1*2 0 =8+2+1 =11 10
  44. 44. Binary Number System  Weights 4 Weight = (Base) Position  Magnitude  Formal Notation Groups of bits 1 1/2 1/4 1 0 1 0 1 2 -1 -2 1 0 =1 *2 2 +0 *2 1 +1 *2 0 +0 *2 -1 +1 *2 -2 Sum of “Bit x Weight”  2 =(5.25) 10 =(101.01)2 4 bits = Nibble 8 bits = Byte 1011 11000101
  45. 45. Binary Number System Binary number Value encoded by the binary number 0 0×2 0 = 0 1 1×2 0 = 1 10 1×2 1 + 0 ×2 0 = 2 11 1×2 1 + 1 ×2 0 = 3 1010 1×2 3 + 0×22 + 1×21 + 0×20 = 8 + 2 = 10 Base-2 scientific notation 1) 2.25ten = 10.01two = 10.01two x 20 = 1.001two x 21  normalized Numbers are usually normalized which means that the leading bit is always a 1. 2) 1100.101two Digit 1 1 0 0 . 1 0 1 Weight 23 22 21 20 Binary decimal Point 2-1 2-2 2-3
  46. 46. Standard binary representation Uses the two numbers from 0 to 1 Every column represents a power of 2 Fixed-point representation Uses the two numbers from 0 to 1 Every column represents a power of 2
  47. 47. Positional Value 1)Positional value of the binary number 10112 Rightmost position: positional value of the base (2) raised to the 0 power First Positional value: 1 (1 * 20) Next position: value of 2 raised to the power of 1 (1 * 21) Next position: 2 squared (0 * 22) Next position: 2 to the third (1 * 23) (1 * 20) + (1 * 21) + (0 * 22) + (1 * 23) 2)Positional value of the binary number 1011.0112 as follows
  48. 48. Whole Numbers (Radix = 2): 10112 = 1 × 23 + 0 × 22 + 1 × 21 + 1 × 20 With Fractional Part (Radix = 2): 11.012 = 1 × 21 + 1 × 20 + 0 × 2-1 + 1 × 2-2 General Case (Radix = R): (S1S0.S-1S-2)R = S1 × R1 + S0 × R0 + S-1 × R -1 + S-2 × R-2 Real's
  49. 49. Example The following shows that the number (11001)2 in binary is the same as 25 in decimal. The subscript 2 shows that the base is 2. The equivalent decimal number is N = 16 + 8 + 0 + 0 + 1 = 25. Example The following shows that the number (101.11)2 in binary is equal to the number 5.75 in decimal.
  50. 50. Octal
  51. 51. Octal Number Systems  Octal and hexadecimal systems provide a shorthand way to deal with the long strings of 1’s and 0’s in binary.  Octal is base-8 system using the digits 0 to 7. 8 digits { 0, 1, 2, 3, 4, 5, 6, 7 }  To convert to decimal, you can again use a column weighted system Weight = (Base) Position 7512 8 = 7*8 3 + 5*8 2 + 1*8 1 + 2*8 0 = 3914 10  An octal number can easily be converted to binary by replacing each octal digit with the corresponding group of 3 binary digits 7512 8 = 111101001010 2
  52. 52. Octal Number System  Weights Weight = (Base)  Position 64 8 1 5 1 2 2 Magnitude 7 4 -1 1 0 Sum of “Digit x Weight” 5 *82+1 *81+2 *80+7 *8-1+4 *8-2 =(330.9375)10  Formal Notation 1/8 1/64 =(512.74)8 -2
  53. 53. Place values for an integer in the octal system Real's
  54. 54. Octal Number System Binary Decimal Octal 0 0 0 1 1 1 10 2 2 11 3 3 100 4 4 101 5 Octal is used as a shorthand for representing 5 file permissions on UNIX systems. 110 6 For example, file mode rwxr-xr-x would be 0755. 6 111 7 7
  55. 55. Example The following shows that the number (1256)8 in octal is the same as 686 in decimal. Note that the decimal number is N = 512 + 128 + 40 + 6 = 686. Example The weight associated with each digit in the given octal number can be determined by raising 8 to a power equivalent to the position of the digit in the number.
  56. 56. Hexadecimal
  57. 57. Number Systems - Hexadecimal  Hexadecimal is a base-16 system.  It contains the digits 0 to 9 and the letters A to F (16 digit values). 16 digits { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F } Note :- symbols A, B, C, D, E, F are equal to 10, 11, 12, 13, 14 , 15 respectively. The symbols in this system are often referred to as hexadecimal digits  The letters A to F represent the unit values 10 to 15.  This system is often used in programming as a condensed form for binary numbers (0x00FF, 00FF)  To convert to decimal, use a weighted system with powers of 16.  Conversion to binary is done the same way as octal to binary conversions.  This time though the binary digits are organized into groups of 4.  Conversion from binary to hexadecimal involves breaking the bits into groups of 4 and replacing them with the hexadecimal equivalent.
  58. 58. System  Weights   256 Weight = (Base) Position Magnitude  Sum of “Digit x Weight” 1/16 1/256 16 1 1 E 5 7 A 2 0 -1 -2 1 1 *162+14 *161+5 *160+7 *16-1+10 *16-2 =(485.4765625)10  Formal Notation =(1E5.7A)16
  59. 59. Place values for an integer in the hexadecimal system Integers
  60. 60. The Hexadecimal Number System Binary Decimal 0 0 1 1 10 2 11 3 100 4 101 5 110 6 111 7 Hexadecimal 0 1 2 3 4 5 6 7 Binary Decimal 1000 8 1001 9 1010 10 1011 11 1100 12 1101 13 1110 14 1111 15 Hexadecimal 8 9 A B C D E F 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
  61. 61. Hexadecimal System The weight associated with each symbol in the given hexadecimal number can be determined by raising 16 to a power equivalent to the position of the digit in the number. Example Digit 4A90.2BC 4 A 9 0 Weight 163 162 161 160 . 2 B C Hexadecimal Point 16-1 16-2 16-3 Example The following shows that the number (2AE)16 in hexadecimal is equivalent to 686 in decimal. The equivalent decimal number is N = 512 + 160 + 14 = 686.
  62. 62. Hexadecimal System Value of 2001 in Binary, Octal and Hexadecimal
  63. 63. Hexadecimal System
  64. 64. Summary of the four positional systems
  65. 65. 1).Why do we use the decimal system for everyday mathematics? Answer: Fingers and Thumbs 2).Why do we use the binary system for computer mathematics? Answer: Computers use voltage levels to perform mathematics. 0-Volts and 5-Volts correspond to 0’s and 1’s
  66. 66. Common Powers Base 10 Power Preface Symbol Value 10-12 pico p .000000000001 10-9 nano n .000000001 10-6 micro µ .000001 10-3 milli m .001 103 kilo k 1000 106 mega M 1000000 109 giga G 1000000000 1012 tera T 1000000000000
  67. 67. Common Powers  Base 2 Power Preface Symbol Value 210 kilo k 1024 220 mega M 1048576 230 Giga G 1073741824 • What is the value of “k”, “M”, and “G”? • In computing, particularly w.r.t. memory, the base-2 interpretation generally applies
  68. 68. Converting Numbers Between Bases
  69. 69. Conversion Among Bases The possibilities Decimal Octal Binary Hexadecimal
  70. 70. Bases    Can represent any quantity in any base Counting process similar for all bases  Count until highest digit for base reached  Add 1 to next higher position to left  Return to 0 in current position Conversion: map from one base to another  Identities easily calculated  Identities obtained by table lookup
  71. 71. Decimal to Decimal Decimal Octal Binary Hexadecimal
  72. 72. Example Weight 12510 => 5 x 100 2 x 101 1 x 102 Base = 5 = 20 = 100 125
  73. 73. Decimal to Binary Decimal Octal Binary Hexadecimal
  74. 74. Converting From Decimal to Binary • 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
  75. 75. Example 12510 = ?2 2 125 2 62 2 31 2 15 7 2 3 2 1 2 0 1 0 1 1 1 1 1 12510 = 11111012
  76. 76. Example Decimal to Binary 149210 (decimal) = ???2 (binary) Repeated Divide by 2
  77. 77. Example The following shows how to convert 35 in decimal to binary. We start with the number in decimal, we move to the left while continuously finding the quotients and the remainder of division by 2. The result is 35 = (100011) 2. Example Convert the decimal number 0.625 to binary. Since the number 0.625 = (0.101)2 has no integral part, the example shows how the fractional part is calculated.
  78. 78. Decimal (Fraction) to Binary Conversion    Multiply the number by the ‘Base’ (=2) Take the integer (either 0 or 1) as a coefficient Take the resultant fraction and repeat the division Example: (0.625) 10 Integer Fraction 0.625 * 2 = 0.25 * 2 = 0.5 * 2 = Answer: Coefficient a -1 = 1 a -2 = 0 a -3 = 1 1 . 25 0 . 5 1 . 0 (0.625)10 = (0.a-1 a-2 a-3)2 = (0.101)2 MSB LSB
  79. 79. A similar method can be used to convert a decimal fraction to binary when the denominator is a power of two: Exampl e The answer is then (0.011011)2
  80. 80. Example An alternative method for converting a small decimal integer (usually less than 256) to binary is to break the number as the sum of numbers that are equivalent to the binary place values shown:
  81. 81. Binary to Decimal Decimal Octal Binary Hexadecimal
  82. 82. Binary to Decimal  Technique    Multiply each bit by 2n, where n is the “weight” of the bit The weight is the position of the bit, starting from 0 on the right Add the results
  83. 83. Decimal Example Example
  84. 84. Example Bit “0” 1010112 => 1 1 0 1 0 1 x x x x x x 20 21 22 23 24 25 = = = = = = 1 2 0 8 0 32 4310
  85. 85. Example The following shows how to convert the binary number (110.11)2 to decimal: (110.11)2 = 6.75.
  86. 86. Decimal to Octal Decimal Octal Binary Hexadecimal
  87. 87. Example 123410 = ?8 8 8 8 8 1234 154 19 2 0 2 2 3 2 123410 = 23228
  88. 88. Decimal to Octal Conversion Example (175) 10 Quotient Remainder 175 / 8 = 21 / 8 = 2 / 8 = 21 7 2 5 0 2 Answer: Example (0.3125) 10 Answer: a0 = 7 a1 = 5 a2 = 2 (175)10 = (a2 a1 a0)8 = (257)8 Integer Fraction 0.3125 * 8 = 0.5 * 8 = Coefficient 2 . 5 4 . 0 Coefficient a -1 = 2 a -2 = 4 (0.3125)10 = (0.a-1 a-2 a-3)8 = (0.24)8
  89. 89. Example The following shows how to convert 126 in decimal to its equivalent in the octal system. We move to the right while continuously finding the quotients and the remainder of division by 8. The result is 126 = (176)8. Example The following shows how to convert 0.634 to octal using a maximum of four digits. The result is 0.634 = (0.5044)8. Note that we multiple by 8 (base octal).
  90. 90. Octal to Decimal Decimal Octal Binary Hexadecimal
  91. 91. Octal to Decimal  Technique    Multiply each bit by 8 n , where n is the “weight” of the bit The weight is the position of the bit, starting from 0 on the right Add the results 86 85 262144 10 32768 10 84 4096 10 83 512 10 82 64 10 8 10 81 1 10 80
  92. 92. Example The following shows how to convert (724)8 to decimal. Example The following shows how to convert (23.17)8 to decimal. This means that (23.17)8 ≈ 19.234 in decimal. Again, we have rounded up 7 × 8−2 = 0.109375.
  93. 93. Decimal to Hexadecimal Decimal Octal Binary Hexadecimal
  94. 94. Example 123410 = ?16 16 16 16 1234 77 4 0 2 13 = D 4 123410 = 4D216
  95. 95. Example The following shows how we convert 126 in decimal to its equivalent in the hexadecimal system. We move to the right while continuously finding the quotients and the remainder of division by 16. The result is 126 = (7E) 16 Example The following shows how to convert 178.6 in decimal to hexadecimal using only one digit to the right of the decimal point. The result is 178.6 = (B2.9) 16 Note that we divide or multiple by 16 (base hexadecimal).
  96. 96. Hexadecimal to Decimal Decimal Octal Binary Hexadecimal
  97. 97. Hexadecimal to Decimal  Technique    Multiply each bit by 16n, where n is the “weight” of the bit The weight is the position of the bit, starting from 0 on the right Add the results 16 5 16 0 1048576 10 16 4 65536 10 16 3 4096 10 16 2 256 10 16 10 16 1 1 10
  98. 98. Example The following shows how decimal. ABC16 => C x 160 B x 161 A x 162 Example to convert the hexadecimal number (ABC) 16 to = 12 x 1 = 12 = 11 x 16 = 176 = 10 x 256 = 2560 274810 The following shows how to convert the hexadecimal number (1A.23) 16 to decimal. Note :- The result in the decimal notation is not exact, because 3 × 16−2 = 0.01171875. We have rounded this value to three digits (0.012).
  99. 99. Base N to Decimal conversion Decimal to Base N Conversions  To convert from decimal to a different number base such as Octal, Binary or Hexadecimal involves repeated division by that number base  Keep dividing until the quotient is zero  Use the remainders in reverse order as the digits of the converted number
  100. 100. Binary to Octal Decimal Octal Binary Hexadecimal
  101. 101. Binary-octal conversion 8 = 23 Each group of 3 bits represents an octal digit Group bits in threes, starting on right Convert to octal digits
  102. 102. Binary − Octal Conversion Example Octal 1 6 . 2 3 )8 Example 10110101112 = ?8 1 011 010 111 1 3 2 7 010 011 100 5 101 6 ( 2 001 2 )2 000 4 ( 10110.01 Binary 0 Assume Zeros 110 7 111 10110101112 = 13278 Works both ways (Binary to Octal & Octal to Binary)
  103. 103. Example What is the binary equivalent of for (24)8? Solution Write each octal digit as its equivalent bit pattern to get 2 → 010 and 4 → 100 The result is (010100)2. Example Show the octal equivalent of the binary number (101110010) 2. Solution Each group of three bits is translated into one octal digit. The equivalent of each 3-bit group is shown in Table on above page 101 (101110010)2 = (562)8. 110 010
  104. 104. Octal to Binary Decimal Octal Binary Hexadecimal
  105. 105. Octal to Binary • Technique • Convert each octal digit to a 3-bit equivalent binary representation • Useful to represent binary numbers indirectly  Octal and binary are nicely related; i.e. 8 = 23  Each octal digit represent 3 binary digits (bits) Example 7058 = ?2 7 0 5 111 000 101 7058 = 1110001012
  106. 106. Binary to Hexadecimal Decimal Octal Binary Hexadecimal
  107. 107. Binary-hexadecimal conversion Technique Group bits in fours, starting on right Convert to hexadecimal digits
  108. 108. Binary − Hexadecimal Conversion   16 = 24 Each group of 4 bits represents a hexadecimal digit Example: Assume Zeros ( 1 ( 1 0110. 01 6 . 4 )2 )16 Works both ways (Binary to Hex & Hex to Binary)
  109. 109. Example Show the hexadecimal (110011100010)2. equivalent of the binary number Solution We first arrange the binary number in 4-bit patterns: 1100 1110 0010 Note that the leftmost pattern can have one to four bits. We then use the equivalent of each pattern shown in Table 2.2 on page 25 to change the number to hexadecimal: (4E2)16. Example
  110. 110. Hexadecimal to Binary Decimal Octal Binary Hexadecimal
  111. 111. Hexadecimal to Binary   Technique  Convert each hexadecimal digit to a 4-bit equivalent binary representation Useful to represent binary numbers indirectly  Hex and binary are nicely related; i.e. 16 = 2 4  Each hex digit represent 4 binary digits (bits)
  112. 112. Example 10AF16 = ?2 1 0 A F 0001 0000 1010 1111 10AF16 = 00010000101011112 Example What is the binary equivalent of (24C)16? Each hexadecimal digit is converted to 4-bit patterns: 2 → 0010, 4 → 0100, and C → 1100 The result is (001001001100)2
  113. 113. Octal to Hexadecimal Decimal Octal Binary Hexadecimal
  114. 114. Octal-hexadecimal conversion
  115. 115. Conversion  Convert to Binary as an intermediate step Example ( ( 2 6 . 2 0 01 0110.010 0 Assume Zeros ( )8 )2 Assume Zeros 1 6 . 4 )16 Works both ways (Octal to Hex & Hex to Octal)
  116. 116. Example 10768 = ?16 1 0 7 6 001 000 111 110 2 3 E 10768 = 23E16
  117. 117. Decimal, Binary, Octal and Hexadecimal Decimal Binary Octal Hex 00 0000 00 0 01 0001 01 1 02 0010 02 2 03 0011 03 3 04 0100 04 4 05 0101 05 5 06 0110 06 6 07 0111 07 7 08 1000 10 8 09 1001 11 9 10 1010 12 A 11 1011 13 B 12 1100 14 C 13 1101 15 D 14 1110 16 E 15 1111 17 F
  118. 118. Example Find the minimum number of binary digits required to store decimal integers with a maximum of six digits. k = 6, b1 = 10, and b2 = 2. Then x = [ k × (logb1 / logb2) ] = [6 × (1 / 0.30103)]= 20. The largest six-digit decimal number is 999,999 and the largest 20-bit binary number is 1,048,575. Note that the largest number that can be represented by a 19-bit number is 524287, which is smaller than 999,999. We definitely need twenty bits.
  119. 119. Hexadecimal to Octal Decimal Octal Binary Hexadecimal
  120. 120. Conversion  Convert to Binary as an intermediate step Example ( 1 6 . 4 )16 ( 0 0 0 1 0 1 1 0 . 01 0 0 )2 Assume Zeros ( 2 6 . 2 )8 Assume Zeros Works both ways (Octal to Hex & Hex to Octal)
  121. 121. Example 1F0C16 = ?8 1 F 0 C 0001 1111 0000 1100 1 7 4 1 4 1F0C16 = 174148
  122. 122. Exercise – Convert ... Decimal Binary Octal Hexadecimal 33 1110101 703 1AF Decimal Binary Octal Hexadecimal 33 100001 41 21 117 1110101 165 75 451 111000011 703 1C3 431 110101111 657 1AF
  123. 123. Common Powers (1 of 2)  Base 10 Power Preface Symbol Value 10-12 pico p .000000000001 10-9 nano n .000000001 10-6 micro µ .000001 10-3 milli m .001 103 kilo k 1000 106 mega M 1000000 109 giga G 1000000000 1012 tera T 1000000000000
  124. 124. Common Powers (2 of 2)  Base 2 Power Preface Symbol Value 210 kilo k 1024 220 mega M 1048576 230 Giga G 1073741824 • What is the value of “k”, “M”, and “G”? • In computing, particularly w.r.t. memory, the base-2 interpretation generally applies
  125. 125. Common Powers (2 of 2)
  126. 126. 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)
  127. 127. Binary Calculation Adding Subtraction Multiplying Division
  128. 128. Decimal Addition 1 1 Carry 5 + 1 5 5 5 1 0 = Ten ≥ Base Base  Subtract a
  129. 129. Binary Addition  Column Addition Carry 1 1 1 1 1 1 1 1 1 1 0 1 = 61 1 0 1 1 1 = 23 1 0 1 0 1 0 0 = 84 + ≥ (2) 10
  130. 130. Binary Addition
  131. 131. Octal Addition Example a. 123 8 + 321 8 = ? 123 8 +321 8 444 8 c. 733 8 + 74 8 = ? b. 4578 + 2458 = ? 4578 + 2458 7248
  132. 132. Example Hexadecimal Addition a. 33 16 + 47 16 = ? 33 16 + 47 16 7A 16 b. 20D316 + 12BC16 = ? 20D316 + 12BC16 338F16 Note:- 2410 =1A16 c. DF 16 + AB 16 = ? d. ADC 16 + DEF 16 = ?
  133. 133. Addition  Question: What is the result of adding 1 to the largest digit of some number system? • (9)10 + 1 = (10)10 • (7)8 + 1 = (10)8 • (1)2 + 1 = (10)2 • (F)16 + 1 = (10)16  Conclusion: Adding 1 to the largest digit in any number system always has a result of (10) in that number system.
  134. 134. Binary Subtraction Borrow a “Base” when needed Borrow 2 from next line 1 2 0 2 2 0 0 2 1 0 0 1 1 0 1 − = 77 1 0 1 1 1 = 23 0 1 1 0 1 1 0 = 54 (2= 10) 2
  135. 135. Binary Subtraction Perform the binary subtraction of the following numbers: 10101 and 01110 A B A-B Borrow 0 0 0 0 0 1 1 1 1 0 1 0 1 1 0 0
  136. 136. Octal Subtraction a. 524 8 - 167 8 = ? 5 2 4 - (4) 1 3 1 4 8 (8+4) (1+8 ) (12) (9) 6 3 (12) 524 8 - 167 8 = 335 8 7 5 8 8
  137. 137. Octal Subtraction a. 524 8 - 167 8 = ? 5 2 4 - 1 48 (1+8) b. 1678 - 248 = ? (8+4) (12) (4) (9) (12) 1 6 78 3 3 c. 1523 8 - 364 8 = ? 524 8 - 167 8 = 335 8 58 167 8 24 8 143 8
  138. 138. Hexadecimal Subtraction a . 44 16 - 17 16 =? 3 4 (3) - 1 2 2 44 16 - 17 16 = 2D 16 4 16 (16 + 4) 16 (20) 16 7 16 (13) D 16 16
  139. 139. Hexadecimal Subtraction b. 20D316 - 12BC16 = ? a. 44 16 - 17 16 =? 4 3 (3) - 1 2 2 4 16 (16 + 4) (20) 7 16 (13) D 16 44 16 - 17 16 = 2D 16 c. DF 16 - AB 16 = ? 20D316 - 12BC16 0E1716
  140. 140. Binary Multiplication Bit by bit Example 1 0 1 1 1 x 1 0 1 0 0 0 0 0 0 1 0 1 1 1 0 0 0 0 0 1 0 1 1 1 1 1 1 0 0 1 1 0
  141. 141. Binary Multiplication Example : Perform the binary multiplication of the decimal numbers 12 and 10. The equivalent binary representation of the decimal number 12 is 1100. The equivalent binary representation of the decimal number 10 is 1010
  142. 142. Octal Multiplication Example 25 8 * 16 8 = ? Method 1 25 8 * 16 8 =446 8
  143. 143. Octal Multiplication Method 2
  144. 144. Hexadecimal Multiplication Example EF * D = ? EF * D = C23
  145. 145. Hexadecimal Multiplication 2F * 1A =?
  146. 146. Hexadecimal mortification table
  147. 147. Binary Division  • • Binary division is also performed in the same way as we perform decimal division. Like decimal division, we also need to follow the binary subtraction rules while performing the binary division. The dividend involved in binary division should be greater than the divisor. The following are the two important points, which need to be remembered while performing the binary division. If the remainder obtained by the division process is greater than or equal to the divisor, put 1 in the quotient and perform the binary subtraction. If the remainder obtained by the division process is less than the divisor, put 0 in the quotient and append the next most significant digit from the dividend to the remainder.
  148. 148. Binary Division Example The equivalent binary representation of the decimal number 18 is 10010. The equivalent binary representation of the decimal number 8 is 1000.
  149. 149. Example Binary Division Find the quotient and remainder when 1111101 is divided by 1101 in modulo 2 arithmetic. We find the quotient is 1011, and the remainder is 0010. This procedure is very useful to us in calculating CRC syndromes
  150. 150. Example Binary Division
  151. 151. Example Binary Division
  152. 152. Example Binary Division
  153. 153. Example Binary Division
  154. 154. Example Binary Division
  155. 155. Example Octal Division 73 8 ÷ 4 8 = ? 73 8 ÷ 4 8 = 16 8 remain 3
  156. 156. Example Octal Division 17534 8 ÷ 64 8 = ? 64 8 × 2 = 150 8 64 8 × 3 = 234 8
  157. 157. Example Hexadecimal Division AB 16 ÷ 3 16 = ? AB 16 ÷ 3 16 = 39 16 AB 16 ÷ C 16 = ? AB 16 ÷ C 16 = E.25 16
  158. 158. Example Hexadecimal Division DEF 16 ÷ AB 16 = ? Note :- Short method AB 16 =10*16 + 11 160+11 171 DE 16 = 13*16 + 14 208 + 14 222 51*16+F=816 + 15 = 831 171*4 = 5C4 171*E = 142E 171*2 = 2E2
  159. 159. Summary of the Division
  160. 160. Bitwise logic operations
  161. 161. What is an Operator?     Operator is an operation performed over data at runtime  Takes one or more arguments (operands)  Produces a new value Operators have precedence  Precedence defines which will be evaluated first Expressions are sequences of operators and operands that are evaluated to a single value Operators can be classified according to  the type of their operands and of their output  Arithmetic  Relational  Logical  Bitwise  the number of their operands  Unary (one operand)  Binary (two operands)
  162. 162. Categories of Operators in C# Category Operators Arithmetic + - * / % ++ -- Relational < <= > >= == != Logical && || ^ ! Binary & | ^ ~ << >> Comparison == != < > <= >= Assignment = += -= *= /= %= &= |= ^= <<= >>= String concatenation + Type conversion is as typeof Other . [] () ?: new
  163. 163. Unary Expression Binary expression
  164. 164. Relational operators    These perform comparisons and the result is what is called a boolean: a value TRUE or FALSE FALSE is represented by 0; anything else is TRUE The relational operators are:       < <= > >= == != (less than) (less than or equal to) (greater than) (greater than or equal to) (equal to) (not equal to)
  165. 165. Logical Operators (also called Boolean operators)   These have Boolean operands and the result is also a Boolean. The basic Boolean operators are: && (logical AND)   || (logical OR) ! (logical NOT) -- unary Operator ! turns true to false and false to true Behavior of the operators &&, || and ^ (1 == true, 0 == false) Example :bool a = true; bool b = false; (a && b) False (a || b) True (a ^ b) True (!b) True (b || true) True (b && true) False (a || true) True (a && true) True (!a) False ((5>7) ^ (a==b)) False
  166. 166. Logical Operators operators) (also called Boolean
  167. 167. Logical Operators (also called Boolean operators) Operation && && && && Operand1 0 0 1 1 Operand2 0 1 0 1 Result 0 0 0 1 Operation ^ ^ ^ ^ Operand1 0 0 1 1 Operand2 0 1 0 1 Result 0 1 1 0 Operation || || || || Operand1 0 0 1 1 Operand2 0 1 0 1 Result 0 1 1 1
  168. 168. BITWISE OPERATORS Bitwise operators operate on individual bits of integer (int and long) values. If an operand is shorter than an int, it is promoted to int before doing the operations. Negative integers are store in two's complement form. For example, -4 is 1111 1111 1111 1111 1111 1111 1111 1100. Bitwise operator ~ turns all 0 to 1 and all 1 to 0 expressions but bit by bit Like ! for boolean The operators |, & and ^ behave like ||, && and ^ for Boolean expressions but bit by bit The << and >> move the bits (left or right) Bitwise operators are used on integer numbers (byte, sbyte, int, uint, long, ulong) Bitwise operators are applied bit by bit
  169. 169. Bitwise Operator Operator Value ~ Bitwise unary NOT & Bitwise AND | Bitwise OR ^ Bitwise XOR >> >>> Shift Right Shift Right zero fill << Shift left & = Bitwise AND Assignment |= Bitwise OR Assignment ^= Bitwise XOR Assignment >>= >>>= <<= Shift Right Assignment Shift Right zero fill Assignment Shift Left Assignment
  170. 170. Bitwise Operator { NOT A } as ∼A { A AND B } as A AND B=A × B A & B=A × B A ∼A 0 1 1 NOT A = 0 A B A×B 0 0 0 0 1 0 1 0 0 1 1 1
  171. 171. Bitwise Operator { A OR B } as follows A OR B=A+B A | B=A+B A B A+B { A OR B OR C} as follows A OR B OR C =A+B+C A | B | C= A+B+C A B C A+B+C 0 0 0 0 0 0 1 1 0 0 0 0 1 1 0 1 0 1 0 1 1 1 1 0 1 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1
  172. 172. Bitwise Operator { A XOR B } as A XOR B = (∼A)B + A(∼B) A ^ B = ( ∼A)B + A(∼B) B A⊗B 0 0 0 0 1 1 1 0 1 1 Summary A 1 0
  173. 173. BITWISE OPERATORS Ope ration | | | | & & & & ^ ^ ^ ^ Ope rand1 0 0 1 1 0 0 1 1 0 0 1 1 Ope rand2 0 1 0 1 0 1 0 1 0 1 0 1 Re s ult 0 1 1 1 0 0 0 1 0 1 1 0 Examples: a = 3; is 00000011 b = 5; is 00000101 ( a | b); // 00000111 ( a & b); // 00000001 ( a ^ b); // 00000110 (~a & b); // 00000100 ( a<<1 ); // 00000110 ( a>>1 ); // 00000001
  174. 174. Bitwise Operator
  175. 175. Bitwise Operator
  176. 176. Bitwise Operator
  177. 177. Bitwise Operator
  178. 178. <-------Bitwise Logical Operators-------> Example :- The binary value of a = 0010 and b = 0111 1.The Bitwise OR : a | b = 7 2.The Bitwise AND : a & b = 2 3.The Bitwise XOR(exclusive OR) : a ^ b = 5 4.The Bitwise unary NOT : ~a & a = 0 5.~a&b|a&~b = 5 <-------Bitewise Shift Operators-------> Example :- The original binary value of a = 0010 and Decimal value of a = 2 1.The Left shift : a = 8 2. The Right shift : b = b >> 2 = 1 3.The original decimal value of u = -1 4. The Unsigned Right shift : u = u >>> 30 means u = 11111111 11111111 11111111 11111111 >>> 30 hence u = 0011 and Decimal value of u = 3 <-------Bitewise Assignment Operators-------> Example :- The original binary value of p = 0101 and Decimal value of p = 5 1.The Bitewise Shift Right Assignment Operators : p >>= 2 means p = p >> hence p =0101 >> 2 so p = 0001 and Decimal value of p = 1
  179. 179. Bitwise Operator Bitwise operators are used in 1.Communication stacks where the individual bits in the header attached to the data signify important information 2.Embedded software for controlling different functions in the chip and indicating the status of hardware by manipulating the individual bits of hardware registers of embedded microcontrollers 3.Low-level programming for applications such as device drivers, cryptographic software, video decoding software, memory allocators, compression software and graphics 4.Maintaining large sets of integers efficiently in search and optimization problems 5.Bitwise operations performed on bit flags, which can enable an instance of enumeration type to store any combination of values defined in an enumerator list
  180. 180. Low level languages are: 1 - Machine Language 2 - Assembly Language 3 - C (C is not the 100% Low-level language) High level languages are: 1 - Visual Basic 2 - Pascal 3 - Java 4 - C++ and many more.

×