Understanding Bit Masking and Bitwise Operations for Efficient Bit Manipulation

Slide Note
Embed
Share

Bit masking is a powerful technique in programming to selectively access or modify specific bits without affecting others. By creating a byte with bits set at desired positions using a bit mask, bitwise operators like OR (|) and AND (&) can be leveraged to efficiently set or clear specific bits. This process is illustrated through examples of setting pins to be outputs, turning on pull-up resistors for certain pins, and clearing specific bits without impacting others. Understanding these concepts is essential for efficient bit manipulation in various programming scenarios.


Uploaded on Sep 29, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Bit Masking To access or affect only the bits we want, we need to construct a byte with bits set in the locations of interest This byte is called a bit mask Use the bit mask with the appropriate bit-wise operator to get the desired result: To set bits: bit-wise OR ( | ) with the bit mask To clear bits: bit-wise AND ( & ) with the negated bit mask

  2. Bit Masking Example 1 Set bits 3, 5, and 7 in DDRD (make pins to be OUTPUTS) without affecting the other bits (port-style): recall: DDRD = 0b10101000; but this would make pins 6, 4, 2, 1, and 0 to be INPUTS, which might not be what we want Instead: 1. Construct a bit mask with 1s in bit positions 3, 5, and 7 and 0s everywhere else 2. Bit-wise OR with DDRD and assign back to DDRD

  3. Bit-Wise OR and AND Bit-wise OR (X | Y) Bit-wise AND (X & Y) X Y Z X Y Z 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 1 0 1 1 1 1 1 1 1

  4. Bit Masking Example 1, cont. (setting bits 3, 5, and 7 in DDRD) 1. Bit mask with bits set in positions 3, 5, and 7: Many ways to do this: 1) Write directly in binary: 0b10101000 2) Write in hex or decimal: 0xA8 or 168 3) Left-shift with OR: (1<<7 | 1<<5 | 1<<3) 2. Bit-wise OR with DDRD and assign back DDRD = DDRD | (bit mask from step 1) DDRD = DDRD | (1<<7 | 1<<5 | 1<<3); DDRD | = (1<<7 | 1<<5 | 1<<3); // shortcut

  5. Bit Masking Example 2 Your turn: turn on the pull-up resistors for pins 1, 4, and 6 (assuming that they are already made to be INPUTS) without affecting the other pins PORTD | = ( (1<<1) | (1<<4) | (1<<6) );

  6. Bit Masking Example 3 Turn off the pull-up resistors for pins 1, 4, and 6 without affecting the other pins Need to clear bits 1, 4, and 6 in PORTD register without affecting the other bits 1. Construct a bit mask with 1s in bit positions 1, 4, and 6 2. Bit-wise AND PORTD with the negated bit mask and re-assign to PORTD

  7. Negate Bits Negate bits with the ~ operator ~ 01010010 10101101

  8. Bit Masking Example 3, cont. (clear bits 1, 4, and 6 in PORTD) Your turn: 1. Construct a bit mask with 1s in bit positions 1, 4, and 6 2. Bit-wise AND with the negated bit mask: byte bit_mask = (1<<6) | (1<<4) | (1<<1); PORTD &= ~bit_mask;

  9. Summary So Far To set bits port-style: 1. Construct a bit-mask with bits in the location(s) of interest 2. Execute bit-wise OR ( | )with assignment back to the register of interest To clear bits port-style: 1. Construct a bit-mask with bits in the location(s) of interest 2. Execute bit-wise AND ( & ) (with the negated bit mask) with assignment back to the register of interest

  10. Determining if a bit is set or cleared Arduino style (easy!): if( digitalRead(pin) ) Port-style: 1. Construct a bit mask with bits in the locations of interest 2. Bit-wise AND the PINx register with the bit mask 3. Test the result Example: Is a SPST switch on pin D1 closed?

  11. Example for determining if a bit is set or cleared Suppose a SPST switch between pin D1 and ground Take action if it is closed Arduino style (easy!) if ( digitalRead(pin_D1) == LOW ){ do stuff; } Port-style: Construct the bit mask: byte bit_mask = ( 1<<1 ); Bit-wise AND the bit mask with the PIND register Test the result if ( (PIND & bit_mask) == bit_mask ){ do stuff; }

Related


More Related Content