90 likes | 335 Views
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;
E N D
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;
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)
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 00001000 VAR= VAR | 0x08; To set bit 3 to '0' the mask is 11110111 VAR = VAR & 0xf7;
More examples • For 32 bit integer data types:FIO2PIN |= 0x00040000; // ?FIO2PIN &= 0xFFFBFFFF;FIO2PIN &= ~0x00040000; • Changing several bits in one operation • FIO2PIN |= 0x00FF0000; • FIO2PIN |= 0x00001010; • PIO2PIN &= ~ 0x00001010; • Toggling bits using XOR (^) • FIO2PIN ^= 0x00000080
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 00001000 (0x08) • Two possible results 00000000 or 00001000 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) { ; }
More examples if ((FIO2PIN & 0x00000080) == 0) { FIO2PIN |= 0x00000001; } if (FIO2PIN & 0x00000004) != 0) { FIO2PIN &= ~0x00000008; } if(FIO2PIN & 0x000000F0) == 0x000000F0) { FIO2PIN ^= 0x00000002; }
Shift Operations • << shift left • 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.
Shift operations FIO2PIN |= (1<<7); // set bit 700000000000000000000000000000001 : start00000000000000000000000000000010 : 100000000000000000000000000000100 : 200000000000000000000000000001000 : 300000000000000000000000000010000 : 400000000000000000000000000100000 : 500000000000000000000000001000000 : 600000000000000000000000001000000 : 7 FIO2PIN &= ~(1<<23); // clear bit 23
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