In the embedded programming, the master bit operation in the operation of the register is very convenient, because before the upper operation of the time, but because of the difficulty of the bit operation is not very large, their own programming, by the way to do some summary.
& | ~ These three bit arithmetic symbols are not difficult to understand, but to distinguish between the logical operation symbols && and | |
1. What needs to be summed up is: if you want to make the register value of 1, it is generally used the value of this register to | 1 For example, to change the value of I to 1 can be used I |= 1; This means that I will be given the value of I and the value above 1. Similarly, if you want to change the value of a variable to 0, use & 0 for example i&=0;
2. However, the general register is represented by the 16 binary, if you want to make the register n bit 1 to the 1<<n bit again | This number can be. if you want to make the register n bit 0 to be the 1<<n bit,, take the inverse & this number
1#include <stdio.h>2 3 intMain ()4 { 5unsigned i=0x4590ab34;//01000 0101 1001 0000 1010 1011 0011 0100 6 7 intj=0x37;//0011 0111 8 intk=0x42;//0100 0010 9unsigned untj=0x37;//0011 0111 Ten Oneprintf"j&k is%x\n", j&k);//0000 0010 Aprintf"J|k is%x\n", j|k);//0111 0111 -printf"~j%x\n", ~j);// -printf"~UNTJ%x\n", ~UNTJ);//1100 C8 the -unsigned ret=0; - //Place the 4th position of I 1 -RET = i| (1<<3); +printf"i is%x\n", i); -printf"ret is%x\n", ret); + A //Place the 2nd position of I 0 atret = i& ~ (1<<2); -printf"i is%x\n", i); -printf"ret is%x\n", ret); - - //Place I on the 4th---9 1 -RET = i| (0x3f<<4);//0011 1111 =3f inprintf"i is%x\n", i); -printf"ret is%x\n", ret); to + //Place I on the 4th---9 0 -ret = i&~ (0x3f<<4);//0011 1111 =3f theprintf"i is%x\n", i); *printf"ret is%x\n", ret); $ Panax Notoginseng //Place I on the 4th---7 1 and 12--15 1 -RET = i| (0xf<<4|0xf<< A) ;//1111 =f theprintf"i is%x\n", i); +printf"ret is%x\n", ret); A the //Place I on the 4th---9 0 and 13--15 0 +ret = i&~ (0x3f<<4|0x7<< -) ;//00111111 3f 0111 =7 -printf"i is%x\n", i); $printf"ret is%x\n", ret); $ - }~
Can.
Here's the code for the test
Embedded C language bit operation &..|. ~.>>