1 / 13

Bitwise Operations

Bitwise Operations. 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.

morna
Download Presentation

Bitwise Operations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Bitwise Operations

  2. 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

  3. 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

  4. Typical Bit-processing Functions • Test, set, reset, or flip (toggle) bits • Extract a subset of bits as a number

  5. 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)

  6. 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)

  7. Setting a BitUse the |= operator • OR the 1-mask into the number • x |= mask

  8. 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

  9. 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

  10. 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

  11. Copying a Bit • If the desired value is 0, reset the bit • Otherwise set it • No short-cut!

  12. Flipping BitsXOR 1’s into the number • Form the appropriate 1-mask • x ^= mask

  13. 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

More Related