Presentation is loading. Please wait.

Presentation is loading. Please wait.

20. LOW-LEVEL PROGRAMMING. Bitwise Shift Operators The bitwise shift operators are: << left shift >> right shift The expression i << j represents i shifted.

Similar presentations


Presentation on theme: "20. LOW-LEVEL PROGRAMMING. Bitwise Shift Operators The bitwise shift operators are: << left shift >> right shift The expression i << j represents i shifted."— Presentation transcript:

1 20. LOW-LEVEL PROGRAMMING

2 Bitwise Shift Operators The bitwise shift operators are: << left shift >> right shift The expression i << j represents i shifted left j positions, zero-filled. The expression i >> j represents i shifted right j positions. If i is of an unsigned type or if the value of i is not negative, then zero bits are added at the left as needed. If i is negative, the result depends on the implementation.

3 Bitwise Shift Operators For portability, it’s best to perform shifts only on unsigned numbers. Examples: unsigned short int i, j; i = 13; /* i is now 13 (binary 0000000000001101) */ j = i << 2; /* j is now 52 (binary 0000000000110100) */ j = i >> 2; /* j is now 3 (binary 0000000000000011) */

4 Other Bitwise Operators The other bitwise operators are: ~ bitwise not & bitwise and ^ bitwise xor | bitwise or

5 Other Bitwise Operators Examples: unsigned short int i, j, k; i = 21; /* i is now 21 (binary 0000000000010101) */ j = 56; /* j is now 56 (binary 0000000000111000) */ k = ~i; /* k is now 65514 (binary 1111111111101010) */ k = i & j; /* k is now 16 (binary 0000000000010000) */ k = i ^ j; /* k is now 45 (binary 0000000000101101) */ k = i | j; /* k is now 61 (binary 0000000000111101) */ Warning: Don’t confuse the bitwise operators & and | with the logical operators && and ||.

6 Using the Bitwise Operators to Access Bits Setting a bit. To set a bit, use a “mask” that contains a 1 bit in the desired position: i |= 0x0010; /* sets bit 4 */ If the position of the bit is stored in a variable, use a shift operator to create the mask: i |= 1 << j; /* sets bit j */

7 Using the Bitwise Operators to Access Bits Clearing a bit. To clear a bit, use a mask that contains a 0 bit in the position to be cleared: i &= 0xffef; /* clears bit 4 */ An alternative: i &= ~ 0x0010; /* clears bit 4 */ The latter form is particularly useful if the position of the bit is stored in a variable: i &= ~ (1 << j); /* clears bit j */

8 Using the Bitwise Operators to Access Bits Testing a bit. Testing a bit requires a mask containing a 1 bit: if (i & 0x0010) … /* tests bit 4 */ To test whether bit j is set, use the following statement: if (i & 1 << j) … /* tests bit j */

9 Using the Bitwise Operators to Access Bit-Fields Dealing with several consecutive bits (a bit-field) is slightly more complicated. Modifying a bit-field. Use a mask containing 0 bits in each position to be modified: i = i & 0xff8f | 0x0050; /* stores 101 in bits 4-6 */ If the new value of the bit-field is stored in a variable, the variable must be shifted before the | operator is applied: i = i & 0xff8f | j << 4; /* stores j in bits 4-6 */ The << operator has higher precedence than & and |, so this statement is equivalent to i = (i & 0xff8f) | (j << 4);

10 Using the Bitwise Operators to Access Bit-Fields Retrieving a bit-field. Retrieving a bit-field at the right end of a variable is easy: j = i & 0x0007; /* retrieves bits 0-2 */ If the bit-field is not at the right end, extract the field using the & operator, then shift the result to the right: j = (i & 0x0070) >> 4; /* retrieves bits 4-6 */

11 Bit-Fields in Structures A structure may contain bit-fields—members whose size is specified as a number of bits: struct file_date { unsigned int day: 5; unsigned int month: 4; unsigned int year: 7; }; The order in which bit-fields are allocated within a word depends on the implementation. Visual C++ allocates bit-fields from right to left (the first bit- field occupies the low-order bits of the word):

12 Bit-Fields in Structures A bit-field can be used just like any other member of a structure: struct file_date fd; fd.day = 28; fd.month = 12; fd.year = 8; After these assignments, fd has the following appearance: Bit-fields must have type int, unsigned int, or signed int. (Using int by itself is ambiguous; some compilers treat the high-order bit as a sign bit, while others don’t.)


Download ppt "20. LOW-LEVEL PROGRAMMING. Bitwise Shift Operators The bitwise shift operators are: << left shift >> right shift The expression i << j represents i shifted."

Similar presentations


Ads by Google