
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
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages63 Page
-
File Size-