Download presentation
Presentation is loading. Please wait.
1
24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05
2
24/06/2015CSE1303 Part B lecture notes 2 Last time Floating point Anatomy of floating point sign exponent mantissa Floating point arithmetic multiplication addition Limitations of floating point
3
24/06/2015CSE1303 Part B lecture notes 3 In this lecture Working with large data byte order Working with small data bitwise operations AND, OR, NOT, XORAND, OR, NOT, XOR maskingmasking shifting relationship to multiplication and divisionrelationship to multiplication and division
4
24/06/2015CSE1303 Part B lecture notes 4 Byte order Problem memory is addressed by bytes (8 bits) how to store a 32-bit value in memory? 0x13579BCF 32-bit value (written in hex for compact- ness, but really in binary) addresses1248 1249 1250 1251...... ? 8 bits
5
24/06/2015CSE1303 Part B lecture notes 5 Byte order Solution break up value into byte-sized chunks store each chunk in successive bytes 0x13579BCF each hex digit represents four bits, so two hex digits to a byte addresses1248 1249 1250 1251...... one byte here next byte here another byte here final byte here 8 bits
6
24/06/2015CSE1303 Part B lecture notes 6 Byte order Which order to store the bytes? starting at the most significant (“big”) end? addresses1248 1249 1250 1251...... 0x13579BCF 0x13 0x57 0x9B 0xCF work this way
7
24/06/2015CSE1303 Part B lecture notes 7 Byte order Which order to store the bytes? starting at the least significant (“little”) end? addresses1248 1249 1250 1251...... 0x13579BCF 0x13 0x57 0x9B 0xCF work this way
8
24/06/2015CSE1303 Part B lecture notes 8 Byte order Two common ways of storing word into memory Big Endian store most significant byte in lowest address store least significant byte in highest address Little Endian store least significant byte in lowest address store most significant byte in highest address
9
24/06/2015CSE1303 Part B lecture notes 9 Byte order Which byte order is used depends on the computer’s Central Processing Unit (CPU) Big Endian Motorola 680x0 (Amiga/Atari ST/Mac classic)Motorola 680x0 (Amiga/Atari ST/Mac classic) IBM/Motorola PowerPC (current Macintosh)IBM/Motorola PowerPC (current Macintosh) MIPS (SGI Indy/Nintendo 64)MIPS (SGI Indy/Nintendo 64) Motorola 680X familyMotorola 680X family Little Endian Intel 80x86/Pentium (IBM PC compatible)Intel 80x86/Pentium (IBM PC compatible) Rockwell 6502 (Commodore 64)Rockwell 6502 (Commodore 64) MIPS (Sony Playstation/Digital DECstation)MIPS (Sony Playstation/Digital DECstation) Some processors (e.g., MIPS) can be configured to be either Big Endian or Little Endian
10
24/06/2015CSE1303 Part B lecture notes 10 Logic operations Boolean logic operations operate on one or two binary values (true/false, 1/0) logical AND (on two values) C operator: &&C operator: && result is true if both operands are true, else falseresult is true if both operands are true, else false logical OR (on two values) C operator: ||C operator: || result is true if either operand is true, else falseresult is true if either operand is true, else false logical NOT (on one value) C operator: !C operator: ! invert operand: true falseinvert operand: true false
11
24/06/2015CSE1303 Part B lecture notes 11 Bitwise operations Bitwise logic operations operate on each bit of one or two values bitwise AND (on two values) for each bit: result is 1 if both operands are 1, else 0for each bit: result is 1 if both operands are 1, else 0 bitwise OR (on two values) for each bit: result is 1 if either operand is 1, else 0for each bit: result is 1 if either operand is 1, else 0 bitwise Exclusive OR (“XOR”) (on two values) for each bit: result is 1 if either operand (but not both) is 1, else 0for each bit: result is 1 if either operand (but not both) is 1, else 0 bitwise NOT (on one value) for each bit: invert (or “flip”) the bit: 1 0for each bit: invert (or “flip”) the bit: 1 0 like flip in “flip_and_add_1” negation of signed valuelike flip in “flip_and_add_1” negation of signed value
12
24/06/2015CSE1303 Part B lecture notes 12 Bitwise operations: NOT Bitwise NOT Invert every bit in a value 01101010 10010101 each bit inverted independently of the others truth table in out 01 10
13
24/06/2015CSE1303 Part B lecture notes 13 Bitwise operations: AND Bitwise AND performs logical AND on corresponding bits of two values result is 1 if both corresponding input bits are 1, 0 otherwise truth table 01101010 11000011 01000010 in 1 in 2 out 000 010 100 111
14
24/06/2015CSE1303 Part B lecture notes 14 Bitwise operations: OR Bitwise OR performs logical OR on corresponding bits of two values result is 1 if either corresponding input bit (or both) are 1, 0 otherwise truth table 01101010 11000011 11101011 in 1 in 2 out 000 011 101 111
15
24/06/2015CSE1303 Part B lecture notes 15 Bitwise operations: XOR Bitwise Exclusive OR (XOR) performs logical Exclusive OR (XOR) on corresponding bits of two values result is 1 if either corresponding input bit (but not both) are 1, 0 otherwise truth table 01101010 11000011 10101001 in 1 in 2 out 000 011 101 110
16
24/06/2015CSE1303 Part B lecture notes 16 Bitwise operations Some operations don’t affect bits y AND 1 = y y OR 0 = y y XOR 0 = y Some operations force bits to 0 or 1 y AND 0 = 0 y OR 1 = 1 Some operations change bits y XOR 1 = not_y
17
24/06/2015CSE1303 Part B lecture notes 17 Extracting bits Sometimes need to isolate one or more bits in a value e.g., find sign in signed int / float 10011010 extract this bit to find out if value is positive or negative but need to ignore all the other bits
18
24/06/2015CSE1303 Part B lecture notes 18 Extracting bits Use bitwise AND to extract bits for desired bits, AND with 1 0 AND 1 0, 1 AND 1 10 AND 1 0, 1 AND 1 1 bit remains unchangedbit remains unchanged for other bits, AND with 0 0 AND 0 0, 1 AND 0 00 AND 0 0, 1 AND 0 0 bit is cleared (set to 0)bit is cleared (set to 0) only desired bits remain, others are cleared
19
24/06/2015CSE1303 Part B lecture notes 19 Extracting bits Example: extract sign bit of C float sign bit AND 00110101111001101101000110011000 10000000000000000000000000000000 00000000000000000000000000000000 a 1 in this column allows the sign bit through zeroes in these columns stop any value from getting through resulting value is zero: number is positive
20
24/06/2015CSE1303 Part B lecture notes 20 Extracting bits Example: extract sign bit of C float AND 00110101111001101101000110011000 10000000000000000000000000000000 a value like this which is used to extract, set or clear individual bits is usually called a mask
21
24/06/2015CSE1303 Part B lecture notes 21 Clearing bits Sometimes need to clear a bit to zero e.g., clear sign bit of a float, to make positive (like fabs function) Use bitwise AND to clear bits for bits to clear, AND with 0 0 AND 0 0, 1 AND 0 00 AND 0 0, 1 AND 0 0 bit is cleared (set to 0)bit is cleared (set to 0) for other bits, AND with 1 0 AND 1 0, 1 AND 1 10 AND 1 0, 1 AND 1 1 bit is unchangedbit is unchanged desired bits are cleared to zero, the other remain unchanged
22
24/06/2015CSE1303 Part B lecture notes 22 Clearing bits Example: set sign bit of C float to 0 sign bit AND 10110101111001101101000110011000 01111111111111111111111111111111 00110101111001101101000110011000 a 0 in this column forces the sign bit to zero ones in these columns allow the value to show through resulting number is positive: 0 in MSB
23
24/06/2015CSE1303 Part B lecture notes 23 Setting bits Sometimes need to set a bit to one e.g., set sign bit of a float, to make negative Use bitwise OR to set bits for bits to set, OR with 1 0 OR 1 1, 1 OR 1 10 OR 1 1, 1 OR 1 1 bit is set (to 1)bit is set (to 1) for other bits, OR with 0 0 OR 0 0, 1 OR 0 10 OR 0 0, 1 OR 0 1 bit is unchangedbit is unchanged desired bits are set to one, others remain unchanged
24
24/06/2015CSE1303 Part B lecture notes 24 Setting bits Example: set sign bit of C float to 1 sign bit OR 00110101111001101101000110011000 10000000000000000000000000000000 10110101111001101101000110011000 a 1 in this column forces the sign bit to one zeroes in these columns allow the value to show through resulting number is negative: 1 in MSB
25
24/06/2015CSE1303 Part B lecture notes 25 Toggling bits Sometimes need to flip a bit’s value e.g., toggle sign bit to negate a float Use bitwise XOR (exclusive OR) to toggle bits for bits to toggle, XOR with 1 0 XOR 1 1, 1 XOR 1 00 XOR 1 1, 1 XOR 1 0 bit is flipped (0 1)bit is flipped (0 1) for other bits, XOR with 0 0 XOR 0 0, 1 XOR 0 10 XOR 0 0, 1 XOR 0 1 bit is unchangedbit is unchanged desired bits are flipped, others remain unchanged
26
24/06/2015CSE1303 Part B lecture notes 26 Toggling bits Example: negate a C float sign bit XOR 10110101111001101101000110011000 10000000000000000000000000000000 00110101111001101101000110011000 a 1 in this column causes the sign bit to flip zeroes in these columns cause the original bit to remain the same resulting number has opposite sign bit
27
24/06/2015CSE1303 Part B lecture notes 27 Bitwise operations in C bitwise AND (on two values) C operator: & (ampersand) bitwise OR (on two values) C operator: | (vertical bar) bitwise Exclusive OR (XOR) (on two values) C operator: ^ (caret) bitwise NOT (on one value) C operator: ~ (tilde)
28
24/06/2015CSE1303 Part B lecture notes 28 Bitwise operations in C /* Examples of bitwise operations. */ int main() { /* 16-bit value, binary 0000001101011110. */ /* 16-bit value, binary 0000001101011110. */ unsigned short value = 0x035E; unsigned short value = 0x035E; /* 16-bit mask, binary 0000000011111111. */ /* 16-bit mask, binary 0000000011111111. */ unsigned short mask = 0x00FF; unsigned short mask = 0x00FF; /* %04X printf format prints as 4 hex digits*/ /* %04X printf format prints as 4 hex digits*/ printf("%04X\n", value ); /* 035E */ printf("%04X\n", value ); /* 035E */ printf("%04X\n", mask ); /* 00FF */ printf("%04X\n", mask ); /* 00FF */ printf("%04X\n", ~value ); /* FCA1 */ printf("%04X\n", ~value ); /* FCA1 */ printf("%04X\n", value & mask ); /* 005E */ printf("%04X\n", value & mask ); /* 005E */ printf("%04X\n", value | mask ); /* 03FF */ printf("%04X\n", value | mask ); /* 03FF */ printf("%04X\n", value ^ mask ); /* 03A1 */ printf("%04X\n", value ^ mask ); /* 03A1 */ return 0; return 0;}
29
24/06/2015CSE1303 Part B lecture notes 29 Shifting Example: extracting exponent from a C float AND 00110101111001101101000110011000 01111111100000000000000000000000 00110101100000000000000000000000 value mask 0000000000000000000000001101011 if you use a mask to get the exponent... 897581056 10... you get this: 107 10 really want this: really want this: shift right 23 bits
30
24/06/2015CSE1303 Part B lecture notes 30 Shifting Shifting is the moving of patterns of bits left or right within a word left or right 0 ≤ amount to shift < word size 01101010 01010000 example: shifting left by three bits fill with zeroes these bits are shifted “off the end” and are discarded
31
24/06/2015CSE1303 Part B lecture notes 31 Shifting in C Binary (two value) operators Shift left operator: << Shift right operator: >> Value to shift goes on left of the operator Amount to shift goes on the right of the operator
32
24/06/2015CSE1303 Part B lecture notes 32 Shifting in C /* Example using C shift operators. */ int main() { /* 16-bit value, binary 1000111100000010. */ /* 16-bit value, binary 1000111100000010. */ unsigned short value = 0x8F02; unsigned short value = 0x8F02; int i = 4; /* used as a shift amount */ int i = 4; /* used as a shift amount */ /* Shift right or left by 4 bits. */ /* Shift right or left by 4 bits. */ printf("%04X\n", value >> 4 ); /* 08F0 */ printf("%04X\n", value >> 4 ); /* 08F0 */ printf("%04X\n", value << 4 ); /* F020 */ printf("%04X\n", value << 4 ); /* F020 */ /* Either side can be variable or literal. */ /* Either side can be variable or literal. */ printf("%04X\n", value >> i ); /* 08F0 */ printf("%04X\n", value >> i ); /* 08F0 */ /* Shifting by 1 harder to see in hex; /* Shifting by 1 harder to see in hex; write these in binary to see what’s happening. */ write these in binary to see what’s happening. */ printf("%04X\n", value >> 1 ); /* 4781 */ printf("%04X\n", value >> 1 ); /* 4781 */ printf("%04X\n", value << 1 ); /* 1E04 */ printf("%04X\n", value << 1 ); /* 1E04 */ return 0; return 0;}
33
24/06/2015CSE1303 Part B lecture notes 33 Shifting and multiplication 8-bit unsigned binary 00001001 00010010 9 10 18 10 00100100 36 10 01001000 72 10 shift left 1 x2 ×2 ×2
34
24/06/2015CSE1303 Part B lecture notes 34 Shifting and division 8-bit unsigned binary 01100100 00110010 100 10 50 10 00011001 25 10 00001100 12 10 shift right 1 ÷2 ÷ 2
35
24/06/2015CSE1303 Part B lecture notes 35 Shifting Shifting left a value by n places is equivalent to multiplying by 2 n << 1 × 2; << 2 × 4; << 3 × 8; etc. Shifting right a value by n places is equivalent to dividing by 2 n >> 1 ÷ 2; etc. Shifting is quicker than multiplication/division for efficiency, compiler will replace multiplication or division by constant power of two with a shift left or shift right
36
24/06/2015CSE1303 Part B lecture notes 36 Shifting signed values 8-bit signed (two’s complement) 01100000 00001100 96 10 12 10 shift right 3 ÷8 MSB = 0: positive fill with 0
37
24/06/2015CSE1303 Part B lecture notes 37 Shifting signed values 8-bit signed (two’s complement) 10100000 00010100 –96 10 +20 10 shift right 3 ÷8 MSB = 1: negative fill with 0 got 00010100 2, wanted –12 10 = 11110100 2 : differs in first three bits
38
24/06/2015CSE1303 Part B lecture notes 38 Shifting signed values 8-bit signed (two’s complement) 10100000 11110100 –96 10 –12 10 shift right 3 ÷8 MSB = 1: negative fill with copies of original MSB by filling with copies of the MSB, positive numbers stay positive, and negative numbers stay negative
39
24/06/2015CSE1303 Part B lecture notes 39 Shifting Need to distinguish two kinds of right shift logical: always fill left bits with 0 arithmetic: fill left bits with copies of MSB C >> operator is logical for unsigned values, unknown for signed values gcc uses arithmetic right shift on signed valuesgcc uses arithmetic right shift on signed values Not a problem with left shift logical left shift same as arithmetic left shift signed: overflow occurred if MSB changes unsigned: overflow if a 1 bit “exits stage left”
40
24/06/2015CSE1303 Part B lecture notes 40 Covered in this lecture Byte order bitwise operations AND, OR, NOT, XOR masking shifting relationship to multiplication and division right shifts arithmetic shift rightarithmetic shift right logical shift rightlogical shift right
41
24/06/2015CSE1303 Part B lecture notes 41 Going further Boolean algebra the mathematics of binary values Digital logic hardware for doing AND, OR,... Bit fields another way of referring to bits in C
42
24/06/2015CSE1303 Part B lecture notes 42 Next time Pointers Aggregation of data arrays structs Characters ASCII Strings Reading: Lecture notes section B06
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.