C--bit arithmetic

Source: Internet
Author: User
Tags arithmetic bitwise

C-The bitwise operation of a bit is the operation of a bit (bit). In the section "Binary thinking and data storage", the bit is an electronic component with 8 bits constituting a byte (byte), which is already the smallest operational unit of granularity.

The C language provides six bit operators:
Operator & | ^ ~ << >>
Description Bitwise-AND Bitwise OR Bitwise XOR OR Take counter Move left Move right
Bitwise AND operation of a bit (bit) bit only 0 and 12 values, only participate in &The two bits of the operation are 1 o'clock, the result is 1, otherwise 0. For example 1&1 for 1,0&0 for 0,1&0 for 0.

Values exist in memory in binary form, 9&5The writable formula is as follows:
00001001 (9 binary)
&AMP;00000101 (5 binary)
00000001 (1 binary)
So 9&5=1
strictly speaking, the value in memory in the complement form, the complement of integers and its binary form is the same, negative numbers are not the same, do not understand the reader can self-complement.
Bitwise-AND operator &All bits of two numbers participating in the operation are &Operation.

Bitwise AND operations are usually used to clear some bits by 0 or to preserve certain bits. For example, C's high 16-bit clear 0, reserved low 16-bit, can be used as a&65535Operation (65536 takes 4 bytes and the binary number is 00000000000000001111111111111111).

Example bit operation example.
  1. #include <stdio.h>
  2. int main(){
  3. unsigned a=9; //binary number 00001001
  4. unsigned b=5; //binary number 00000101
  5. unsigned c=0xde09a32b; //decimal number 3725173547
  6. unsigned d=0x0000ffff; //decimal number 65535
  7. printf("a=%u, b=%u, a&b=%u\ n", a, b, a&b);
  8. printf("c=%u, d=%u, C&d (%%d) =%u, C&d (%%x) =%x\ n", C, D, C&d , C&d);
  9. return 0;
  10. }
Operation Result:
A=9, B=5, a&b=1
c=3725173547, d=65535, C&d (%d) =41771, C&d (%x) =a32b
bitwise OR operation participation or operation |Of the two bits have one for 1 o'clock, the result is 1, two are 0 o'clock the result is 0. For example 1|1 for 1,0|0 for 0,1|0 for 1.

9|5The writable formula is as follows:
00001001 (9 binary)
|00000101 (5 binary)
00001101 (13 binary)
So 9|5=13

A bitwise OR operation can be used to place some binary positions 1, while some bits are reserved.

examples, or examples of operations.
  1. #include <stdio.h>
  2. int main(){
  3. unsigned a=9; //binary number 00001001
  4. unsigned b=5; //binary number 00000101
  5. Unsigned c=0xde09a30b; //decimal number 3725173547
  6. Unsigned d=0xffff0000; //decimal number 65535
  7. printf("a=%u, b=%u, a|b=%u\ n", a, b, a|b);
  8. printf("c=%u, d=%u, C|d (%%d) =%u, C|d (%%x) =%x\ n", C, D, C|d, C| D);
  9. return 0;
  10. }
Operation Result:
A=9, B=5, a|b=13
c=3725173515, d=4294901760, C|d (%d) =4294943499, C|d (%x) =ffffa30b
Bitwise XOR OR operation participating in an XOR operation ^The two bits are not the same, the result is 1, the same result is 0. That is, 0^1 is 1,0^0 for 0,1^1 0.

9^5Can be written as follows:
00001001 (9 binary)
^00000101 (5 binary)
00001100 (12 binary)
So 9^5=12

Bitwise XOR or operations can be used to reverse certain bits.

Example XOR operation example.
  1. #include <stdio.h>
  2. int main(){
  3. unsigned a=9; //binary number 00001001
  4. unsigned b=5; //binary number 00000101
  5. unsigned c=0x00ffff00; //decimal number 3725173547
  6. unsigned d=0xffff0000; //decimal number 65535
  7. printf("a=%u, b=%u, a^b=%u\ n", a, b, a^b);
  8. printf("c=%u, d=%u, C^d (%%d) =%u, C^d (%%x) =%x\ n", C, D, C^d, c^d);
  9. return 0;
  10. }
Operation Result:
A=9, B=5, a^b=12
c=16776960, d=4294901760, C^d (%d) =4278255360, C^d (%x) =ff00ff00
Take inverse operation to negation operator ~As a single-mesh operator, right-associative, the function is to take the number of participating operations of the binary bitwise negation. For example, the 0,~0 is 1.

~9The operation is:
~0000000000001001
1111111111110110
So ~9=65526
Left shift operator left shift <<It is used to shift all the operands of the operand to the left several bits, the high drop, the low 0. <<On the left is the operand to shift,<< to the right is the number of bits to move. For example:
a=9;a<<3;
The above code means moving the binary of a to the left by 3 bits. a=00001001 (binary of 9), 01001000 (decimal 72) After moving left 3 bits.
Right-shift operation right-shift operator >>It is used to shift all the binary of the operand to the right of a number of bits, drop discard, high 0 (or 1). For example:
a=9;a>>3;
Represents moving the binary of a to the right by 3 bits. A=00001001 (9 binary), 3 digits to the right and 00000001 (decimal 1).

It is important to note that for signed numbers, when you move right, the symbol bits are moved. When positive, the highest bit is 0, while negative, the sign bit is 1, the highest bit is 0 or the complement 1 depends on the compiler's provisions.

The sample bit operation synthesizes the example.
  1. #include <stdio.h>
  2. int main(){
  3. unsigned c=0x00ffff00; //decimal number 3725173547
  4. unsigned d=0xffff0000; //decimal number 65535
  5. printf("c=%x, d=%x, C^d (%%x) =%x, C|d (%%x) =%x, c>>4=%x, c<<8=%x\ n", C, D, C ^d, c|d, c>>4, c<<8);
  6. return 0;
  7. }
Operation Result:
C=FFFF00, d=ffff0000, C^d (%x) =ff00ff00, C|d (%x) =ffffff00, c>>4=ffff0, c<<8=ffff0000

C--bit arithmetic

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.