The bitwise operation of C and or non-XOR or

Source: Internet
Author: User
Tags bit set

Bit operations are more easily mixed:The bitwise operation of C and or non-XOR or and arithmetic:& Both are 1 for 1, otherwise 0
1&1=1, 1&0=0, 0&1=0, 0&0=0
or operation:| both are 0 for 0, otherwise 1 1|1 = 1, 1|0 = 1, 0|1 = 1, 0|0 = 0
non-arithmetic:~ 1 Take 0, 0 take 1 to = 0, ~0 = 1 ~ (10001) = 01110
xor operation:^ The two are equal to 0, unequal to 1 (easy to confuse ) 1^1=0, 1 ^ 0=1, 0 ^ 1=1, 0^0=0
displacement operator:<< and >>

A bitwise operation is a binary-based operation. In a program, it is often necessary to deal with bits problems. The C + + language provides 6 bit manipulation operators. These operators can only be used for integer operands, that is, only for signed or unsigned char,short,int and long types.

In practical applications, it is recommended to use the unsigned integer operand, because the signed operand may be different depending on the machine result.

The bitwise operator operators for C + + and C are identical, but the Bitset standard library is added in C + + to support bit set operations, please refer to http://book.51cto.com/art/200809/88698.htm or http:// Book.csdn.net/bookfiles/17/1001760.shtml, these links are "C + + Primer Chinese Version" of the content, you can also read directly.

The following is a list of C/s + + bit manipulation operators, where operator precedence is descending from top to bottom, but the <<,>> priority is the same.

A/C + + bit manipulation operator
Operator Function Usage
~ Bit negation ~expr
<< Move left Expr1 << EXPR2
>> Move right Expr1 >> EXPR2
& Bit and Expr1 & EXPR2
^ Bit XOR or Expr1 ^ expr2
| Bit or Expr1 | Expr2

The code examples are as follows:

[CPP]View Plaincopyprint?
  1. #include <iostream>
  2. using namespace std;
  3. int Main () {
  4. unsigned short x=3,y=5;
  5. cout<<"~x="<< (unsigned short) ~x<<endl; //bit negation
  6. cout<<"~x="<<~x<<endl; //bit negation
  7. cout<<"x&y="<< (x&y) <<endl; //bit and
  8. cout<<"x^y="<< (x^y) <<endl; //Bit XOR or
  9. cout<<"x|y="<< (x|y) <<endl; //bit or
  10. cout<<"x<<1="<< (x<<1) <<endl; //bit left shift
  11. cout<<"y>>1="<< (y>>1) <<endl; //bit right shift
  12. return 0;
  13. }
<textarea name="code" class="cpp" style="display: none;" rows="15" cols="50">#include <iostream>using namespace Std;int main () {unsigned short x=3,y=5;cout<< "~x=" << ( unsigned short) ~x<<endl;//-cout<< "~x=" <<~x<<endl;//-bit negation cout<< "x&y=" << ( X&y) <<endl;//bit with cout<< "x^y=" << (x^y) <<endl;//bit xor cout<< "x|y=" << (X|y) < <endl;//bit or cout<< "x<<1=" << (x<<1) <<endl;//bit left shift cout<< "y>>1=" << ( y>>1) <<endl;//bit right shift return 0;}</textarea>

The results of the operation are as follows:

~x=65532

~x=-4

X&y=1

X^y=6

X|y=7

X<<1=6

y>>1=2

The code is interpreted as follows:
Short is a 16-bit integer, so the binary representation of x, Y is as follows:
X=3 (00000000 00000011)
Y=5 (00000000 00000101)

~ 00000000 00000011
= 11111111 11111100 (65532 or-4) (as to why the same bits represents a different number, this is related to the numerical representation of the computer, for specific reasons to search for "complement")

00000000 00000011
& 00000000 00000101
= 00000000 00000001 (1)

00000000 00000011
^ 00000000 00000101
= 00000000 00000110 (6)

00000000 00000011
| 00000000 00000101
= 00000000 00000111 (7)

00000000 00000011<<1
= 00000000 00000110 (6)

00000000 00000101>>1
= 00000000 00000010 (2)

Example reference: bitwise operator (i): c/C + + bitwise operator-Jason's Column-Blog channel-csdn.net
http://blog.csdn.net/jason314/article/details/5719933


The bitwise operation of C and or non-XOR or

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.