Download presentation
Presentation is loading. Please wait.
Published byUrsula Dorsey Modified over 9 years ago
2
| bit OR & bit AND ~ bit NOT ^ bit EXLUSIVE OR (XOR) > bit RIGHT SHIFT
3
These operators work on bits and not logical values. Take two 8 bit bytes, combine with any of these operators, and you will get another 8 bit byte according to the operator's function. These operators work on the individual bits inside the byte. A truth table helps to explain each operation. In a truth table, a 1 bit stands for true, and a 0 stands for false.
4
0 OR 0 = 0 0 OR 1 = 1 1 OR 0 = 1 1 OR 1 = 1 Symbol used : | Example : 00001111|11110000 = 11111111
5
0 AND 0 = 0 0 AND 1 = 0 1 AND 0 = 0 1 AND 1 = 1 Symbol used : & Example : 00001111 & 11110000 =00000000
6
0 XOR 0 = 0 0 XOR 1 = 1 1 XOR 0 = 1 1 XOR 1 = 0 Symbol used: ^ Example : 00001111 ^ 10101010 = 10100101
7
The NOT operator inverts the sense of the bit, so a 1 becomes a 0, and a 0 becomes a 1. Symbol used: ~ Example: ~(00001111)= 11110000
8
To set pin 2: (0x01 << 2) To set pin 7: (0x01 << 7)
9
Internal peripherals send and receive information from the processor using registers. Example: DDRX, PORTX, PINX, etc X=A,B,C,D.
10
// LED Blink Program using Software Delay #include //header file containing in-built delay functions #include //contains inbuilt input and output operations void main() { DDRD = 0b11111111; // Make Port B as Output: for(;;) // endless loop { PORTD = 0b10000000; // led ON _delay_ms(100); PORTD = 0b00000000; // led OFF _delay_ms(100); }
11
So let's say I have a byte foo that is initialized to 0: Code: unsigned char foo = 0; To set bit 0 in foo and then store the result back into foo: Code: foo = foo | 00000001; The OR operation is used between the variable that we want to change and a constant which is called a BIT MASK or simply the MASK. The mask is used to identify the bit that we want changed. we can also write the constants in hexadecimal because it's shorter than writing it in binary
12
How to Convert hexa decimal to binary and vice versa ??? foo |= 0x01; To clear bit 0 in foo requires 2 bit operators: foo = foo & ~0x01; Again, the statement is made shorter as follows: Code: foo &= ~0x01; There is another useful tool that is not often seen, and that is when you want to flip a bit, but you don't know and you don't care what state the bit is currently in, which you will be seeing at the end.
13
// LED Blink Program using Software Delay #include //header file containing in-built delay functions #include //contains inbuilt input and output operations void main() { DDRD = 0b11111111; // Make Port B as Output: for(;;) // endless loop { PORTD| = 0b10000000; // led ON (except pin 7 rest all are masked ) _delay_ms(100); PORTD &= 0b01111111; // led OFF (except pin 7 rest all are masked) _delay_ms(100); } }
14
// LED Blink Program using Software Delay #include //header file containing in-built delay functions #include //contains inbuilt input and output operations void main() { DDRD = 0b11111111; // Make Port B as Output: PORTD=0b00000000; for(;;) // endless loop { PORTD^=0b10000000; _delay_ms(100); } }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.