SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding Bitwise Operators and Shifts in Low-Level Programming

teresa avatar
12 diapositives · 233 Views · Oct 12, 2014

Learn about bitwise shift operators, bitwise not, and, xor, and or operators in low-level programming. Examples provided for setting, clearing, testing bits, and accessing bit-fields in structures.

Understanding Bitwise Operators and Shifts in Low-Level Programming - slide 1
01 / 12Understanding Bitwise Operators and Shifts in Low-Level Programming
1 / 17

Télécharger la présentation

Understanding Bitwise Operators and Shifts in Low-Level Programming
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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.)