1 / 9

Bit Manipulation in 'C'

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;

levi
Télécharger la présentation

Bit Manipulation in 'C'

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

  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 00001000 VAR= VAR | 0x08; To set bit 3 to '0' the mask is 11110111 VAR = VAR & 0xf7;

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

  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 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) { ; }

  6. More examples if ((FIO2PIN & 0x00000080) == 0) { FIO2PIN |= 0x00000001; } if (FIO2PIN & 0x00000004) != 0) { FIO2PIN &= ~0x00000008; } if(FIO2PIN & 0x000000F0) == 0x000000F0) { FIO2PIN ^= 0x00000002; }

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

  8. Shift operations FIO2PIN |= (1<<7); // set bit 700000000000000000000000000000001 : start00000000000000000000000000000010 : 100000000000000000000000000000100 : 200000000000000000000000000001000 : 300000000000000000000000000010000 : 400000000000000000000000000100000 : 500000000000000000000000001000000 : 600000000000000000000000001000000 : 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

More Related