... /34 CSCI 222: Intermediate Programming / C Chapter 5 Alice E. Fischer October 9 and 2, 25
... 2/34 Outline Integer Representations Binary Integers Integer Types Bit Operations Applying Bit Operations Count the Bits Reverse the Bit Order Calculate the Parity of the Bits
... 3/34 Integer Representations I. Integer Representations Binary Integers Input Conversion Signed and Unsigned Integers Integer Notation Writing Numbers in Hex Formats for Unsigned Numbers
... 4/34 Integer Representations Binary Integers Representation - a Review Everything in a computer is represented by bits. All integers are stored in binary, even when we write them in base 8 or or 6. Place values for base 6 (hexadecimal) are shown on the left; base-2 (binary) place values are on the right. Each hexadecimal digit occupies the same memory space as four binary bits because 2 4 = 6. 6 3 = 496 6 2 = 256 6 = 6 6 = 2 5 32768 2 3 892 2 248 2 9 52 2 7 28 2 5 32 2 3 8 2 2 2 4 6384 2 2 496 2 24 2 8 256 2 6 64 2 4 6 2 2 4 2
... 5/34 Integer Representations Binary Integers Input Conversion: When scanf() reads a number, the ASCII representation of that number must be converted to binary for internal storage. Suppose we have a file buffer that contains two numbers. The first one has been read already and we are ready to read in the second number with %i format. Here is what happens:. Skip whitespace, initialize answer to. 2. Loop until scanner is NOT pointing at a digit. 2. Multiply the previous answer by 2.2 Calculate the integer corresponding to the ascii character by subtracting. 2.3 Add the value of the digit to the answer. 2.4 Increment the scanner.
... 6/34 Integer Representations Binary Integers Input Conversion: 2 Initially: 2 2 9 5 3 6 \ scanner? answer while (isdigit (*scanner)) { answer *= ; answer += (*scanner++ - ); } Step: Step2: 2 2 9 5 3 6 \ scanner 2 2 9 5 3 6 \ scanner answer 3 answer
... 7/34 Integer Representations Binary Integers Input Conversion: 3 2 2 9 5 3 6 \ scanner 2 2 9 5 3 6 \ scanner 2 2 9 5 3 6 \ scanner 3 3 answer 3 36 answer 36 answer
... 8/34 Integer Representations Integer Types Long, Short, and Int The c99 standard defines integers in four lengths. Int can be the same as short or long. Char can be either signed or unsigned. Common Name Full Name Normal Length char signed char byte unsigned char short signed short int 2 bytes unsigned short unsigned short int int signed int 2 or 4 bytes unsigned unsigned int long signed long int 4 bytes unsigned long unsigned long int long long signed long long int 4 or 8 bytes unsigned short unsigned long long int
... 9/34 Integer Representations Integer Types Signed and Unsigned Integers In a signed integer, the leftmost bit is the sign bit. A in this position has a large negative value. The other positions have positive values, as shown. The binary representations of several signed and unsigned integers follow. Several of these values turn up frequently during debugging, so it is useful to be able to recognize them. 2 5 = 32768 2 4 = 6384 2 3 = 892 2 2 = 496 2 = 248 2 = 24 2 9 = 52 2 8 = 256 2 7 = 28 2 6 = 64 2 5 = 32 2 4 = 6 2 3 = 8 2 2 = 4 2 = 2 2 = Interpreted as a signed short int high-order bit = -32768 Interpreted as an unsigned short int high-order bit = 32768 32767-32768 + 32767 = - -32768 + 22768 = - -32768 + = -32768-32768 + = -32767 32767 +32768 + 32767 = 65535 +32768 + 22768 = 55536 +32768 + = 32768 +32768 + = 32769
... /34 Integer Representations Integer Types Using Unsigned Types Sometimes a program must deal directly with hardware components or the actual pattern of bits stored in the memory. Applications include Cryptography, where you work with bits, not numbers. Random number generation Programs that control hardware switches Sometimes the data is naturally unsigned, as in digital photography.
... /34 Integer Representations Integer Types Integer Notation C provides more than one way to write an integer number because: Base- notation is very inconvenient for many applications that rely on unsigned numbers. Base does not correspond in an easy way to the internal representation of numbers or to their length in bytes. It takes calculation starting with a base- number, to arrive at its binary representation.
... 2/34 Integer Representations Integer Types Integer Literals There are four ways to write a number in your program (a literal): Decimal integer: Use digits 9 but DO NOT start with. Octal integer: Use digits 7 and always start with. Hexadecimal integer: Use digits 9 and A F or a f and always start with x. Hexadecimal character constant: Start with x, followed by one or two digits 9 and A F or a f and end with. Remember: there are two hex digits per byte in an integer value.
... 3/34 Integer Representations Integer Types Writing Numbers in Hex These numbers are signed short ints. Leading zeros may be either written or omitted. Decimal Hex Decimal Hex Decimal Hex x 7 x 65 x4 7 x7 8 x2 9 x5b 8 x8 3 xe 27 x7f xa 32 x2 28 x8 xb 33 x2 255 xff 5 xf 63 x3f 256 x 6 x 64 x4 32767 x7fff - xffff -2 xfffe -32768 x8
... 4/34 Integer Representations Integer Types Conversion Specifiers for Unsigned Values int long short char Character I/O %c Decimal I/O %u %lu %hu Hex input %i or %x %li or %lx %hi or %hx Hex output %4x or %8x %8lx %4hx %x If the input stream contains numbers in the form of hexadecimal literals: x35, they can be read as hex values using %i. If the x is not part of the input, then %x must be used to read a number as hexadecimal.
... 5/34 Bit Operations II. Bit Operations
... 6/34 Bit Operations Bitwise operators. Arity Symbol Meaning Precedence Unary Bitwise complement 5 Binary << Left shift >> Right shift & Bitwise AND 8 ˆ Bitwise exclusive OR (XOR) 7 Bitwise OR 6
... 7/34 Bit Operations Bitwise Complement Arithmetic negation, logical not, and bitwise complement are three ways to get th opposite of a number. Complement inverts all the bits in the number. To negate a number, complement and add. Decimal Hex Binary Decimal Hex Binary x x x x x xff x 2 xfe -x x -x xff!x x!x x x 5 xf x xf6 x 6 x9 x 9 x9 -x 5 xa -x xa!x x!x x
... 8/34 Bit Operations Bitwise And Use AND to turn bits off or to isolate one part of a number. Binary Hex Binary Hex x xff x x7a mask x2 mask xf x & mask x2 x & mask x7 x x7a x x7a mask xc mask x3 x & mask x8 x & mask x2 You will need the last mask in the steganography program, to isolate pairs of bits from the modified image.
... 9/34 Bit Operations Bitwise Or Use or to turn on bits or to combine parts into a whole. Binary Hex Binary Hex x x x xc mask x2 mask xf x mask x2 x mask xfc x x3e x x9c mask xa7 bits x x mask xbf x bits x9d In the steganography program, you need the last operation to make a byte out of four pairs of bits extracted from the modified photograph.
... 2/34 Bit Operations Exclusive Or Use xor to toggle some of the bits or to test for inequality. Binary Hex Binary Hex x xff x xcc mask x3 mask xf x ^ mask xcf x ^ mask x3c x x7a x x7a y xa3 y x7a x ^ y xd9 x ^ y x
... 2/34 Bit Operations Left Shift Left shift moves the bits of the number to the left; bits that move off the left end are forgotten. bits are pulled in to fill the right end. This is very fast for hardware. Shifting left by one position has the same effect on a number as multiplying by 2 but it is a lot faster for the machine. The table on the next slide shows the results of several shift operations on these variables: signed char s; // A one-byte signed integer. unsigned char u; // A one-byte unsigned integer. Note that he result of n << 2 is four times the value of n, as long as no significant bits fall off the left end.
... 22/34 Bit Operations Left and Right Shifts Signed Decimal Hex Binary s 5 xf s << 2 6 x3c s >> 2 3 x3 s xf6 s << 2 4 xd8 s >> 2 3 xfd Unsigned Decimal Hex Binary u xa u << 2 4 x28 u >> 2 2 x2 u 255 xff u << 2 252 xfc u >> 2 63 x3f
... 23/34 Bit Operations Right Shift The table on the previous slide shows the results of several right shift operations. Right shift moves the bits of the number to the right; bits that move off the right end are forgotten. In a signed right shift, copies of the sign bit are pulled in to fill the left end. For an unsigned right shift, bits are used to fill the left end Shifting right by one position has the same effect on a number as dividing by 2. To enable this, a signed right shift keeps the sign bit unchanged. Note that he result of n >> 2 is one fourth of the value of n.
... 24/34 Applying Bit Operations II. Applying Bit Operations Count the Bits Reverse the Bit Order Calculate the Parity of the Bits
... 25/34 Applying Bit Operations Count the Bits Count the Bits Steps and 2 These drawings go with the popcount3() function in the bitops program. The goal is to count the number of bits in the byte. We execute the function popcount3(x49), which is the bitstring. Initially, count=. st time x The result of an AND is if both bits are. Otherwise it is. x & count += x& 2nd time x >>+ x & count += x& Using as a mask keeps the rightmost bit and zeros out the other seven.
... 26/34 Applying Bit Operations Count the Bits Count the Bits Steps 3 through 5 3rd time x >>+ x & If x ends in a bit, the count is not changed. If it ends in a bit, we add to the bit count. count += x& 4th time x x & Now the second bit has been shifted to the end of the word, so the count grows. count += x& 5th time x >>+ x & count += x& 2 2 Using as a mask keeps the rightmost bit and zeros out the other seven.
... 27/34 Applying Bit Operations Count the Bits Count the Bits Steps 6 through finish 6th time x >>+ x & If x ends in a bit, the count is not changed. If it ends in a bit, we add to the bit count. count += x& 2 7th time x x & Now the second bit has been shifted to the end of the word, so the count grows. count += x& 3 End of loop x >>+ count 3 X has become so we leave the loop and return the count.
... 28/34 Applying Bit Operations Reverse the Bit Order Reverse the Bit Order These drawings go with the Reverse program. The goal is to take a byte and reverse the order of its bits so that the last bit becomes the first bit, etc. The code was written to reverse the bits in all the bytes in a pixel image. The variable p points at the current pixel. p We will trace the reversal process on this pixel, x59.
... 29/34 Applying Bit Operations Reverse the Bit Order First Three Steps. c r st time c & r = (r << ) (c & ) c >>= r 2nd time c & r = (r << ) (c & ) c >>= r 3rd time c & r = (r << ) (c & )
... 3/34 Applying Bit Operations Reverse the Bit Order Steps Four through Six. c >>= r 4th time c & r = (r << ) (c & ) c >>= r 5th time c & r = (r << ) (c & ) c >>= r 6th time c & r = (r << ) (c & )
... 3/34 Applying Bit Operations Reverse the Bit Order Ending the Reversal Loop. c >>= r 7th time c & r = (r << ) (c & ) c >>= r 8th time c & r = (r << ) (c & ) p Original contents of *p p after *p = r
... 32/34 Applying Bit Operations Calculate the Parity of the Bits Parity Steps and 2 These drawings go with the parity3() function in the bitops program. The goal is to calculate whether a byte has odd or even bit parity. We execute the function parity3(x49), which is the bitstring. st time x The result of an XOR is if the bits are parity the same, if they are different. parity ^= x 2nd time x >> parity The rightmost bit of parity will change every time x is shifted and the last bit of x is a. parity ^= x
... 33/34 Applying Bit Operations Calculate the Parity of the Bits Parity Steps 3 through 5 3rd time x >> parity If there are an odd number of bits, we call it "odd parity". parity ^= x 4th time x >> parity If there are an even number of bits, we call it "even parity". parity ^= x 5th time x >> parity parity ^= x If the rightmost bit of x is a, parity does not change.
... 34/34 Applying Bit Operations Calculate the Parity of the Bits Parity Steps 6 through finish 6th time x >> parity parity ^= x 7th time x >> parity parity ^= x When we finish, only the rightmost bit will matter; the others will be masked off. We are done when the last bit is shifted off the end of x. Finish x >> parity parity & The value of x is now zero, so we leave the while loop. Then we mask off all the bits of parity except the final one and return the answer. In this case, we have odd parity.