9
3-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 placesy = 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;

Bit Manipulation in 'C

Embed Size (px)

DESCRIPTION

Bit Manipulation in 'C'. 'C' bit operators OP symbol DescriptionExample &ANDy = y & 0xF7; |ORy = y | 0x08; ^XOR >> n shift bits right n placesy = y >> 4;

Citation preview

Page 1: Bit Manipulation in 'C

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

Page 2: Bit Manipulation in 'C

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

0 0 0

0 1 1

1 0 1

1 1 1

AND

A B Output

0 0 0

0 1 0

1 0 0

1 1 1

XOR

A B Output

0 0 0

0 1 1

1 0 1

1 1 0

Page 3: Bit Manipulation in 'C

3-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 00001000VAR= VAR | 0x08;

To set bit 3 to '0' the mask is 11110111

VAR = VAR & 0xf7;

Page 4: Bit Manipulation in 'C

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

3-4

Page 5: Bit Manipulation in 'C

3-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 bitExample: 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 1while( (VAR & 0x08) == 0) { ; }

Loop while bit is '1', exit when bit becomes 0while( (VAR & 0x08) != 0) { ; }

Page 6: Bit Manipulation in 'C

More examples

if ((FIO2PIN & 0x00000080) == 0)

{

FIO2PIN |= 0x00000001;

}

if (FIO2PIN & 0x00000004) != 0)

{

FIO2PIN &= ~0x00000008;

}

if(FIO2PIN & 0x000000F0) == 0x000000F0)

{

FIO2PIN ^= 0x00000002;

}

3-6

Page 7: Bit Manipulation in 'C

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.

3-7

Page 8: Bit Manipulation in 'C

Shift operations

FIO2PIN |= (1<<7); // set bit 7

00000000000000000000000000000001 : start00000000000000000000000000000010 : 100000000000000000000000000000100 : 200000000000000000000000000001000 : 300000000000000000000000000010000 : 400000000000000000000000000100000 : 500000000000000000000000001000000 : 600000000000000000000000001000000 : 7

FIO2PIN &= ~(1<<23); // clear bit 23

3-8

Page 9: Bit Manipulation in 'C

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: 1. Loop until switch S1 is logic 1

2. Now turn on the led

3. then wait until both switches are at logic 1

4. Now turn on the motor

5. Loop until either of th eswitches is change to logic 0

6. Turn of the motor and the led

3-9