160 likes | 330 Vues
This article delves into the fundamental concept of bitwise operations, crucial for operating and embedded systems programming. It explains various operators such as Bitwise-AND, OR, XOR, and NOT, outlining how they work strictly on integer types. Learn to test, set, reset, and extract bits efficiently, as well as how to handle multiple bits using masking techniques. Discover the significance of unsigned integers to avoid issues like sign extension and integral promotion. With practical examples and clear explanations, this guide is perfect for both novice and experienced programmers.
E N D
Who cares about bits? • Most people don’t! • Operating systems do • Embedded systems programming • Flags, etc. • People short on memory resources do, • Can pack boolean values into an int • Examples: std::bitset • You do! • Since Program 3 requires bitwise operations
Bitwise Operators • Bitwise-OR (|) • Bitwise-AND (&) • Bitwise-XOR (^) (“exclusive-or”) • Bitwise-NOT (~) • Shift-left (<<) • Shift-right (>>) • Only work on integers • Any size (char, short, int, long) • Unsigned integers should be used • To avoid sign extension with >> and integral promotion
Typical Bit-processing Functions • Test, set, reset, or flip (toggle) bits • Extract a subset of bits as a number
Bitwise Principles • ANDing a bit with a 1 reads it (b & 1 == b) • ORing a bit with 1 sets it to 1 (b | 1 == 1) • ANDing a bit with 0 resets it (b & 0 == 0) • XORing a bit with 1 flips it (b ^ 1 == ~b) • ORing a bit with 0 is a nop (b | 0 == b) • ANDing a bit with 1 is a nop (b & 1 == b) • XORing a bit with 0 is a nop (b ^ 0 == b)
Testing a BitUse the & operator • First form a one-mask • Place 1 in the desired bit position (n), zeroes elsewhere: • unsigned mask = 1u << n • Note: bit-0 is on the far right (1’s place) • Then AND it with the number • x & mask • The result is non-zero iff the bit is 1 • To convert the result to 1 or 0: !!(x & mask)
Setting a BitUse the |= operator • OR the 1-mask into the number • x |= mask
Setting Multiple Contiguous bitsOR multiple 1’s into the number • To set all bits: • x = -1, or • x = ~0u (preferred) • Mask to set the lowern bits: • mask = (1u << n) – 1 (preferred), or • mask = ~0u >> (NBITS – n) • NBITS is number bits in an int (= sizeof(int) * CHAR_BIT) • Mask to set bits m through n (inclusive, m < n): • mask = (1u << (n-m+1) – 1) << m
Resetting a BitUse the &= operator • Form a 0-mask • 0 in the bit position, 1’s elsewhere • By flipping the 1-mask: • unsigned mask = ~(1u << n) • Then AND into the number: • x &= mask
Resetting Multiple BitsAND multiple 0’s into the number • Flip the corresponding masks for setting bits • forming 0-masks • Then AND the mask into the number
Copying a Bit • If the desired value is 0, reset the bit • Otherwise set it • No short-cut!
Flipping BitsXOR 1’s into the number • Form the appropriate 1-mask • x ^= mask
Extracting Contiguous Bits as a Number • Form the 1-mask to extract the bits • By ANDing • Shift right the appropriate number of positions • so the right-most bit of interest is in the 1’s place • Example: bitops.cpp • Extracts components of an IEEE float