ACP Course07

Total Page:16

File Type:pdf, Size:1020Kb

ACP Course07 Applied Computer Programming Representation of Numbers. Bitwise Operators Course 07 Lect.eng. Adriana ALBU, PhD Politehnica University Timisoara Internal representation • All data, of any type, processed by a computer must have the same fundamental representation • In order to work correctly with these data, a programmer should understand: – the representation of numbers and their memory storage – the fundamental manner a computer operates with numbers (bitwise operations) Representation of numbers Binary Octal Hexadecimal Representation of numbers • Most of our programs don’t need concern regarding operations at the bit level • We’re free to think in bytes, or integers and doubles, or even higher level data types composed of a combination of these • But there are times when we'd like to be able to go to the level of an individual bit Representation of numbers • Any value (variable or constant) that is processed by our programs needs a memory space and it has a memory representation (sequence of bits) • Bit – unit of data storage – may have one of two values: 0 or 1 – usually is not individually addressable • Byte – unit of addressable data – in all usual computer architectures it contains 8 bits (CHAR_BIT from limits.h specifies its dimension) Representation of numbers • The byte is the lowest level at which we can access data; there's no "bit" type, and we can't ask for an individual bit • In fact, we can't even perform operations on a single bit – every bitwise operator will be applied to, at a minimum, an entire byte at a time • This means we'll be considering the whole representation of a number whenever we talk about applying a bitwise operators Binary representation • Binary numbers – used in mathematics and digital electronics – expressed in the binary numeral system (also called base-2 numeral system) – use two different symbols: 1 (one) and 0 (zero) – these two symbols may also have the meanings of true/false, yes/no, on/off • the binary system is used internally by almost all modern computers and computer-based devices such as mobile phones Binary representation • Any number can be represented by a sequence of bits (binary digits), which in turn may be represented by any mechanism capable of being in two mutually exclusive states • In a computer, the numeric values may be represented by two different voltages Binary representation • When written, binary numerals are often subscripted, prefixed or suffixed in order to indicate their base • The following notations are equivalent: – 11100101 binary (explicit statement of format) – 11100101b (a suffix indicating binary format) – 11100101B (a suffix indicating binary format) – bin 11100101 (a prefix indicating binary format) – 111001012 (a subscript indicating base-2 (binary) notation) – %11100101 (a prefix indicating binary format) – 0b11100101 (a prefix indicating binary format, common in programming languages) – 8b11100101 (a prefix indicating number of bits in binary format, common in programming languages) Binary representation • When spoken, binary numerals are usually read digit-by-digit, in order to distinguish them from decimal numerals – the binary numeral 100 is pronounced one zero zero, rather than one hundred, to make its binary nature explicit, and for purposes of correctness • since the binary numeral 100 represents the value four, it would be confusing to refer to the numeral as one hundred (a word that represents a completely different value, or amount) – alternatively, the binary numeral 100 can be read out as "four" (the correct value), but this does not make its binary nature explicit Counting in binary • Counting in binary is similar to counting in any other number system • Beginning with a single digit, counting proceeds through each symbol, in increasing order • Before examining binary counting, it is useful to briefly discuss the more familiar decimal counting system as a frame of reference Decimal counting • Decimal counting uses the ten symbols 0 through 9 • Counting primarily involves incremental manipulation of the "low-order" digit, or the rightmost digit, often called the "first digit” • When the available symbols for the low-order digit are exhausted, the next-higher-order digit (located one position to the left) is incremented, and counting in the low-order digit starts over at 0 Decimal counting • In decimal, counting proceeds like so: – 000, 001, 002, ... 007, 008, 009, (rightmost digit starts over, and next digit is incremented) – 010, 011, 012, ... – ... – 090, 091, 092, ... 097, 098, 099, (rightmost two digits start over, and next digit is incremented) – 100, 101, 102, ... • After a digit reaches 9, an increment resets it to 0, but also causes an increment of the next digit to the left Binary counting • In binary, counting follows similar procedure, except that only the two symbols 0 and 1 are used • Thus, after a digit reaches 1 in binary, an increment resets it to 0, but also causes an increment of the next digit to the left: – 0000, – 0001, (rightmost digit starts over, and next digit is incremented) – 0010, 0011, (rightmost two digits start over, and next digit is incremented) – 0100, 0101, 0110, 0111, (rightmost three digits start over, and the next digit is incremented) – 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111 ... Binary to decimal • Since binary is a base-2 system, each digit represents an increasing power of 2, with the rightmost digit representing 20, the next representing 21, then 22, and so on • To determine the decimal representation of a binary number simply take the sum of the products of the binary digits and the powers of 2 which they represent • For example, the binary number 100101 is converted to decimal form as follows: 5 4 3 2 1 0 – 1001012 = 1 × 2 + 0 × 2 + 0 × 2 + 1 × 2 + 0 × 2 + 1 × 2 – 1001012 = 1 × 32 + 0 × 16 + 0 × 8 + 1 × 4 + 0 × 2 + 1 × 1 – 1001012 = 3710 Decimal to binary 157 2 156 78 2 • Successive divisions by 2, 1 78 39 2 storing the reminder (0 or 1) 0 38 19 2 • Stop when the quotient is 0 1 18 9 2 1 8 4 2 • The binary result is the 1 4 2 2 sequence of reminders read 0 2 1 2 from the end to the beginning: 0 0 0 => 157 = 10011101 1 10 2 • Validation: 7 4 3 2 0 100111012 = 1 × 2 + 1 × 2 + 1 × 2 + 1 × 2 + 1 × 2 = 128 + 16 + 8 + 4 + 1 = 15710 Decimal to other bases • This method can be modified to convert from decimal to any base • The divisor was 2 because the desired destination was base 2 (binary) • If the desired destination is a different base, replace the 2 in the method with the desired base • For example, if the desired destination is base 8, replace the 2 with 8 • The final result will then be in the desired base Decimal to octal 870 8 864 108 8 • Octal is base-8, with numbers 6 104 13 8 between 0 and 7 4 8 1 8 =>87010 = 15468 5 0 0 1 • common writing in programming languages: prefixed by zero: 01546 • Validation: 3 2 1 0 15468 = 1 × 8 + 5 × 8 + 4 × 8 + 6 × 8 = 512 + 320 + 32 + 6 = 87010 Decimal to hexadecimal • The hexadecimal (base sixteen) numeral system has sixteen possible values, using the letters A, B, C, D, E, and F for the six place-values after 9: Dec 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Hex 0 1 2 3 4 5 6 7 8 9 A B C D E F Oct 0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 Bin 0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 Decimal to hexadecimal 19959 16 19952 1247 16 => 1995910 = 4DF716 7 1232 77 16 • common writing in 15 64 4 16 programming languages: (F) 13 0 0 prefixed by 0x: 0x4DF7 (D) 4 • Validation: 3 2 1 0 4DF716 = 4 × 16 + 13 × 16 + 15 × 16 + 7 × 16 = 16384 + 3328 + 240 + 7 = 1995910 Binary arithmetic • Arithmetic in binary is much like arithmetic in other numeral systems – addition – subtraction – multiplication – division Addition • The simplest arithmetic operation in binary is addition • Adding two single-digit binary numbers is relatively simple, using a form of carrying: – 0 + 0 → 0 Addition table – 0 + 1 → 1 0 1 – 1 + 0 → 1 0 0 1 1 0 1 1 10 – 1 + 1 → 0, carry 1 => 102 (1×2 +0×2 = 210) • Adding two "1" digits produces a digit "0", while 1 will have to be added to the next column Addition • This is similar to what happens in decimal when certain single-digit numbers are added together; if the result equals or exceeds the value of the base (10), the digit to the left is incremented: – 5 + 5 → 0, carry 1 (since 5 + 5 = 10 = 1 × 101 + 0 × 100) – 7 + 9 → 6, carry 1 (since 7 + 9 = 16 = 1 × 101 + 6 × 100) • This is known as carrying • When the result of an addition exceeds the value of a digit, the procedure is to "carry" the excess amount divided by the radix (that is, 10/10) to the left, adding it to the next positional value Addition • Carrying works the same way in 1 1 1 1 1 carried digits binary: 0 1 1 0 1 + – In this example, two numerals are being added together: 1 0 1 1 1 1 0 0 1 0 0 011012 (1310) and 101112 (2310) – The top row shows the carry bits used – Starting in the rightmost column, 1 + 1 = 102; the 1 is carried to the left, and the 0 is written at the bottom of the rightmost column – The second column from the right is added: 1 + 0 + 1 = 102 again; the 1 is carried, and 0 is written at the bottom – The third column: 1 + 1 + 1 = 112; this time, a 1 is carried, and a 1 is written in the bottom row – Proceeding like this gives the final answer 1001002 (3610) Subtraction • Subtraction works in much the same way: starred columns are borrowed from – 0 − 0 → 0 * * * * – 0 − 1 → 1, borrow 1 1 1 0 1 1 1 0 − – 1 − 0 → 1 1 0 1 1 1 – 1 − 1 → 0 1 0 1 0 1 1 1 • Subtracting a "1" digit from a "0" digit produces the digit "1", while 1 will have to
Recommended publications
  • Representing Information in English Until 1598
    In the twenty-first century, digital computers are everywhere. You al- most certainly have one with you now, although you may call it a “phone.” You are more likely to be reading this book on a digital device than on paper. We do so many things with our digital devices that we may forget the original purpose: computation. When Dr. Howard Figure 1-1 Aiken was in charge of the huge, Howard Aiken, center, and Grace Murray Hopper, mechanical Harvard Mark I calcu- on Aiken’s left, with other members of the Bureau of Ordnance Computation Project, in front of the lator during World War II, he Harvard Mark I computer at Harvard University, wasn’t happy unless the machine 1944. was “making numbers,” that is, U.S. Department of Defense completing the numerical calculations needed for the war effort. We still use computers for purely computational tasks, to make numbers. It may be less ob- vious, but the other things for which we use computers today are, underneath, also computational tasks. To make that phone call, your phone, really a com- puter, converts an electrical signal that’s an analog of your voice to numbers, encodes and compresses them, and passes them onward to a cellular radio which it itself is controlled by numbers. Numbers are fundamental to our digital devices, and so to understand those digital devices, we have to understand the numbers that flow through them. 1.1 Numbers as Abstractions It is easy to suppose that as soon as humans began to accumulate possessions, they wanted to count them and make a record of the count.
    [Show full text]
  • Binary Numbers Bob Brown Information Technology Department Southern Polytechnic State University
    Binary Numbers Bob Brown Information Technology Department Southern Polytechnic State University Positional Number Systems The idea of “number” is a mathematical abstraction. To use numbers, we must represent them in some way, whether by piles of pebbles or in some other way. It is common to create a code for representing numbers. Such codes are called number systems. Every number system uses symbols to convey information about the value of a number. A positional (or radix) number system is one in which the value that a symbol contributes to a number is determined by the both symbol itself and the position of the symbol within the number. That's just a fancy way of saying that 300 is far different from 3. Compare the idea of a positional number system with Roman numbers, where X means 10 no matter where in a number it appears. The decimal number system we use every day is a positional number system. Decimal numbers are also called base 10 or radix 10 numbers. The symbols are the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9, the plus and minus signs, and the period or decimal point. The position of each digit within the number tells us the multiplier used with it. Consider the number 1037. We learned in elementary school to call the rightmost digit the ones place, the next digit the tens place, then the hundreds place, then the thousands place, 3 2 1 0 and so on. Mathematically, 1037 means 110 + 010 + 310 + 710 . Each digit in the number is multiplied by some power of ten, starting with 100 at the right and increasing by one for each position to the left.
    [Show full text]
  • On Two Conversion Methods of Decimal-To-Binary
    On Two Conversion Methods of Decimal-to-Binary Zhengjun Cao Department of Mathematics, Shanghai University, Shanghai, China. [email protected] Abstract Decimal-to-binary conversion is important to modern binary computers. The classical method to solve this problem is based on division operation. In this paper, we investigate a decimal-to-binary conversion method based on addition operation. The method is very easily implemented by software. The cost analysis shows that the latter is more preferable than the classical method. Thus the current Input/Output translation hardware to convert between the internal digit pairs and the external standard BCD codes can be reasonably removed. Keywords. Decimal-to-binary conversion; division operation; addition operation. 1 Introduction IF OUR ANCESTORS had invented arithmetic by counting with their two fists or their eight fingers, instead of their ten “digits”, we would never have to worry about writing binary-decimal conversion routines. — Donald E. Knuth arXiv:1308.0555v1 [cs.OH] 1 Aug 2013 We know a digit a ∈ {0, 1, · · · , 9} received from a numeric key on a keyboard (see Picture 1) is represented as a string of 4 bits. The mechanism is usually referred to as Binary Coded Decimal (BCD for short). For instance, the digit 9 is represented as 1001 . When we input the integer 79 using numeric keys ‘7’ and ‘9’, it is first represented as two strings 0111 1001 . We here omit the other leftmost bits in each byte which represent numeric type or sign. The two strings then will be converted into the resulting binary representation 1001111 . In 1988.
    [Show full text]
  • Multi–Precision Math
    Multi–Precision Math Tom St Denis Algonquin College Mads Rasmussen Open Communications Security Greg Rose QUALCOMM Australia August 29, 2017 This text has been placed in the public domain. This text corresponds to the v0.39 release of the LibTomMath project. This text is formatted to the international B5 paper size of 176mm wide by 250mm tall using the LATEX book macro package and the Perl booker package. Contents 1 Introduction 1 1.1 MultiplePrecisionArithmetic . 1 1.1.1 WhatisMultiplePrecisionArithmetic? . 1 1.1.2 The Need for Multiple Precision Arithmetic . 1 1.1.3 BenefitsofMultiplePrecisionArithmetic . 3 1.2 PurposeofThisText......................... 4 1.3 DiscussionandNotation . 5 1.3.1 Notation............................ 5 1.3.2 PrecisionNotation . 5 1.3.3 AlgorithmInputsandOutputs . 6 1.3.4 MathematicalExpressions . 6 1.3.5 WorkEffort.......................... 7 1.4 Exercises ............................... 7 1.5 IntroductiontoLibTomMath . 9 1.5.1 WhatisLibTomMath?. 9 1.5.2 GoalsofLibTomMath . 9 1.6 ChoiceofLibTomMath . 10 1.6.1 CodeBase........................... 10 1.6.2 APISimplicity ........................ 11 1.6.3 Optimizations......................... 11 1.6.4 PortabilityandStability . 11 1.6.5 Choice............................. 12 2 Getting Started 13 2.1 LibraryBasics ............................ 13 2.2 WhatisaMultiplePrecisionInteger? . 14 2.2.1 The mp intStructure .................... 15 iii 2.3 ArgumentPassing .......................... 17 2.4 ReturnValues............................. 18 2.5 InitializationandClearing . 19 2.5.1 Initializing an mp int .................... 19 2.5.2 Clearing an mp int...................... 22 2.6 MaintenanceAlgorithms . 24 2.6.1 Augmenting an mp int’sPrecision . 24 2.6.2 Initializing Variable Precision mp ints ........... 27 2.6.3 Multiple Integer Initializations and Clearings . 29 2.6.4 ClampingExcessDigits . 31 3 Basic Operations 35 3.1 Introduction.............................
    [Show full text]
  • Multi–Precision Math
    Multi–Precision Math Tom St Denis Algonquin College Mads Rasmussen Open Communications Security Greg Rose QUALCOMM Australia March 10, 2007 This text has been placed in the public domain. This text corresponds to the v0.39 release of the LibTomMath project. Tom St Denis 111 Banning Rd Ottawa, Ontario K2L 1C3 Canada Phone: 1-613-836-3160 Email: [email protected] This text is formatted to the international B5 paper size of 176mm wide by 250mm tall using the LATEX book macro package and the Perl booker package. Contents 1 Introduction 1 1.1 Multiple Precision Arithmetic . 1 1.1.1 What is Multiple Precision Arithmetic? . 1 1.1.2 The Need for Multiple Precision Arithmetic . 1 1.1.3 Benefits of Multiple Precision Arithmetic . 3 1.2 PurposeofThisText......................... 4 1.3 DiscussionandNotation . 5 1.3.1 Notation............................ 5 1.3.2 PrecisionNotation . 5 1.3.3 Algorithm Inputs and Outputs . 6 1.3.4 MathematicalExpressions. 6 1.3.5 WorkEffort.......................... 7 1.4 Exercises ............................... 7 1.5 IntroductiontoLibTomMath . 9 1.5.1 WhatisLibTomMath?. 9 1.5.2 GoalsofLibTomMath . 9 1.6 ChoiceofLibTomMath .. .. .. .. .. .. .. .. .. .. 10 1.6.1 CodeBase........................... 10 1.6.2 APISimplicity ........................ 11 1.6.3 Optimizations.. .. .. .. .. .. .. .. .. .. .. 11 1.6.4 Portability and Stability . 11 1.6.5 Choice............................. 12 2 Getting Started 13 2.1 LibraryBasics ............................ 13 2.2 WhatisaMultiplePrecisionInteger? . 14 2.2.1 The mp intStructure .................... 15 iii 2.3 ArgumentPassing .......................... 17 2.4 ReturnValues............................. 18 2.5 InitializationandClearing . 19 2.5.1 Initializing an mp int .................... 19 2.5.2 Clearing an mp int...................... 21 2.6 MaintenanceAlgorithms .
    [Show full text]