Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bit Manipulation in 'C' 'C' bit operators

Similar presentations


Presentation on theme: "Bit Manipulation in 'C' 'C' bit operators"— Presentation transcript:

1 Bit Manipulation in 'C' 'C' bit operators
OP symbol Description Example & AND y = y & 0xF7; | OR y = y | 0x08; ^ XOR >> n shift bits right n places y = y >> 4; << n shift bits left n places y = y << 2; ~ complements (invert all bits) y = ~y; Only use with integer data types - Not real types. Note: In 'C' binary operators(such as y = y OP z;) can be written as in a shortened form: y OP= z; E.g.'s y = y + 7; y+=7; y = y & 0x80; y &= 0x80; y = y >> 4; y >>= 4;

2 Masking for bit manipulation
Masking uses AND and OR operators Use OR ('|') to set bits to '1' Use AND ('&') to set bits to 0 Use XOR(^) to toggle bits (0 -> 1 and 1-> 0) OR A B Output 1 AND A B Output 1 XOR A B Output 1

3 Output Masking Setting bits to 1 using the OR (|) bit-wise operator
General format: value = value | mask; Shorthand : value |= mask; Mask bits : '1' to set a bit, '0' to leave bit unchanged. Setting bits to 0 using the AND (&) bit-wise operator General format: value = value & mask; Shorthand : value &= mask; Mask bits : '0' to clear a bit, '1' to leave bit unchanged Example: Controlling a single bit (Note VAR is an 8-bit integer data type.) To set bit 3 to '1' the mask is VAR= VAR | 0x08; To set bit 3 to '0' the mask is VAR = VAR & 0xf7;

4 More examples For 32 bit integer data types: FIO2PIN |= 0x ; // ? FIO2PIN &= 0xFFFBFFFF; FIO2PIN |= ~0x ; Changing several bits in one operation FIO2PIN |= 0x00FF0000; FIO2PIN |= 0x ; PIO2PIN &= ~ 0x ; Toggling bits using XOR (^) FIO2PIN ^= 0x FIO2PIN |= 0x ; // set bit 18 FIO2PIN &= 0xFFFBFFFF; // clear bit 18 FIO2PIN |= ~0x ; // clear bit 18 Changing multiple bits FIO2PIN |= 0x00FF0000; // set bits 16 thro' 23 FIO2PIN |= 0x ; //set bits 4 and 12 PIO2PIN &= ~ 0x ; //clear bits 4 and 12 Toggling bits using XOR (^) FIO2PIN ^= 0x ; //toggle bit 7

5 Input Masking When a single bit need to be tested
Use AND (&) and a mask to isolate the selected bit value & mask Used in input Polling to test an individual input bit Example: Testing bit 3 if mask is (0x08) Two possible results or when ANDED I.e. zero or non-zero Loop while bit is '0', exit when bit becomes 1 while( (VAR & 0x08) == 0) { ; } Loop while bit is '1', exit when bit becomes 0 while( (VAR & 0x08) != 0) { ; }

6 More examples if ((FIO2PIN & 0x ) == 0) { FIO2PIN |= 0x ; } if (FIO2PIN & 0x ) != 0) FIO2PIN &= ~0x ; if(FIO2PIN & 0x000000F0) == 0x000000F0) FIO2PIN ^= 0x ; If bit 7 is 0 then set bit 0 if bit 2 is 1 then clear bit 3 if bits 4 thro' 7 are ones then toggle bit 1

7 Shift Operations << shift left >> shift right
used to move bits to the left - lose MSB and gain new LSB (new LSB = 0 for all data types) multiplies by 2 for each shift. >> shift right used to move bits to the right - lose LSB and gain new MSB (new MSB = 0 for unsigned integer data types, or previous MSB for signed types). divides by 2 for each shift.

8 Shift operations FIO2PIN |= (1<<7); // set bit : start : : : : : : : 7 FIO2PIN &= ~(1<<23); // clear bit 23

9 Exercise A32 bit register is interfaced to the following:- A motor connected to bit 7 (0 is off, 1 is on) An led connected to bit 0 (0 is off, 1 is on) Two switches S1 and S2 connected to bits 1 and 2 respectively that generate either logic 0 or 1 inputs. Write C code to perform the following steps: Loop until switch S1 is logic 1 Now turn on the led then wait until both switches are at logic 1 Now turn on the motor Loop until either of th eswitches is change to logic 0 Turn of the motor and the led


Download ppt "Bit Manipulation in 'C' 'C' bit operators"

Similar presentations


Ads by Google