-bit operation
Bit operation is a unary and two-dollar operation of the bitwise or binary number of the bit-mode in the program design. On many ancient microprocessors, bit operations
Slightly faster than the addition and subtraction operations, usually the bit operation is much faster than the multiplication algorithm. In modern architectures, this is not the case: the operation speed of bit operations is usually
Same as addition operation (still faster than multiplication).
Bitwise Operators
the inverse (~) is a unary operator that performs a logical counter-operation on each bit of a binary number. The number 10% is 0 and 0 is 1.
Bitwise OR (|) handles two binary numbers with the same length, and as long as one of the two corresponding binaries is 1, the result value of the bit is 1.
Bitwise XOR or (^) is the peer-to- long Binary mode that performs a logical XOR bitwise OR operation on each bit of a bitwise or binary number, and the result of the operation is that if a bit is different then the bit is 1, otherwise the bit is 0.
Bitwise and (&) handle two binary numbers with the same length, two corresponding binary is 1, the result value of this bit is 1, otherwise 0.
Shift
A shift is a two-dollar operator used to move each bit in a binary number to One direction, and the overflow part is discarded, and the vacant part fills in a certain value. In the Class C language, the left shift uses two less than the symbol "<<", and the right shift uses two greater than the symbol ">>" to represent.
The logical shift (either left or right) is 0 of the vacancy.
The arithmetic shift is to ensure that the sign bit does not change (the logical left shift is 0, the logical right shift looks at the sign bit).
For example: 110110011 yes-10110011 the first 1 in front is a sign bit
So 110110011 right shift one should be 111011001.
Here are some simple programming questions for bit arithmetic
Exchange values of two integers without additional variables
#include <iostream>using namespace Std;int fun1 (int a,int b) {int tmp=a-b; tmp>>= (sizeof (int) *4-1); if (TMP) return B; return A;} int main () {int a=-134,b=98; cout<< "Max:" <<fun1 (A, b) <<endl; return 0;}
2. Find out the larger number of two numbers without any comparative judgment
#include <iostream>using namespace std;void fun2 (int &a,int &b) {a=a+b; B=a-b; A=a-b;} int main () {int a=-134,b=98; Fun2 (A, b); cout<< "A:" <<a<< ", B:" <<b<<endl; return 0;}
3. Number of 1 in binary expressions of integers
#include <iostream>using namespace Std;int fun3 (int a) {int tmp,count=0; for (int i=1;i<32;a>>=1,++i) {if (a&1==1) count++; } return count;} int main () {fun4 (); Cout<<fun3 (a) <<endl; return 0;}
4. Given an array of integers ar, Only one of the numbers appears odd number of times, the other numbers are even words, print this number.
#include <iostream>using namespace Std;int fun4 (int *ar,int n) {int tmp=0; for (int i=0;i<n;++i) tmp^=ar[i]; return TMP;} int main () {int ar[]={1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,2}; int n=sizeof (AR)/sizeof (int); cout<< "value is:" <<fun4 (ar,n) <<endl; return 0;}
This article is from "Make Yourself" blog, please make sure to keep this source http://ljl1463.blog.51cto.com/13187791/1954837
C Language review 2 bit operation