C-language bit operation and sample code _c language

Source: Internet
Author: User
Tags bitwise bitwise operators

The so-called bit operation, is to operate on a bit (bit) bits. In the section "Binary thought and data storage," the bit is an electronic component, and 8 bits form a byte, which is already the smallest operational unit of granularity.

The C language provides six kinds of bitwise operators:

Operator & | ^ ~ << >>
Description Bitwise AND by bit or Per-bitwise XOR OR Take the counter Move left Move right

Bitwise AND Operations (&)

A bit bit has only 0 and 12 values, and only two of the participating & operations are 1 o'clock, the result is 1, otherwise 0. For example, 1&1 for 1,0&0 0,1&0 is also 0, which is very similar to logical operator &&.

C language cannot directly use binary,& the operands on either side can be decimal, octal, hexadecimal, which in memory are ultimately stored in binary form,& is the operation of these bits in memory. The same is true of other bitwise operators.

For example, 9 & 5 can be converted to the following operations:

0000 0000--0000 0000--0000 0000--0000 1001 (9 in-memory storage)
& 0000 0000--0000 0000--0000 0000--0000 0101 (5 in-memory storage)
-----------------------------------------------------------------------------------
0000 0000--0000 0000--0000 0000--0000 0001 (1 in-memory storage)

That is, bitwise-and-operation will perform & operations on all bits of the two numbers participating in the operation, and the result of 9 & 5 is 1.

Also, -9 & 5 can be converted to the following operations:

1111 1111--1111 1111--1111 1111--1111 0111 (-9 in-memory storage)
& 0000 0000--0000 0000--0000 0000--0000 0101 (5 in-memory storage)
-----------------------------------------------------------------------------------
0000 0000--0000 0000--0000 0000--0000 0101 (5 in-memory storage)

The result of the -9 & 5 is 5.

With regard to the way in which positive and negative numbers are stored in memory, we have explained them in the VIP tutorial, how integers are stored in memory.

Once again,,& is based on the bits in memory, not the binary form of the data, and the other bitwise operators . In the case of -9&5, 9 of the storage in memory is very different from the binary form of-9:

1111 1111--1111 1111--1111 1111--1111 0111 (-9 in-memory storage)
-0000 0000--0000 0000--0000 0000--0000 1001 (-9 of the binary form, the front of the extra 0 can be wiped off)

Bitwise and operations are usually used to clear bits 0, or to retain certain bits. For example, to put N high 16-bit 0, keep the lower 16 bits, you can do N & 0XFFFF operations (0XFFFF in memory storage form 0000 0000-0000 0000-1111 1111-1111 1111).

"Example" examines the analysis above.

#include <stdio.h>
int main () {
  int n = 0x8fa6002d;
  printf ("%d,%d,%x\n", 9 & 5,-9 & 5, N & 0XFFFF);
  return 0;
}

Run Result:

1, 5, 2D

Bitwise OR Operation (|)

Participating in the operation of the two bits has one 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 1, which is the logical operation of the | | Very similar.

For example, 9 | 5 can be converted into the following operations:

0000 0000--0000 0000--0000 0000--0000 1001 (9 in-memory storage)
| 0000 0000--0000 0000--0000 0000--0000 0101 (5 in-memory storage)
-----------------------------------------------------------------------------------
0000 0000--0000 0000--0000 0000--0000 1101 (13 in-memory storage)

9 | The result of 5 is 13.

Another example,-9 | 5 can be converted into the following operations:

1111 1111--1111 1111--1111 1111--1111 0111 (-9 in-memory storage)
| 0000 0000--0000 0000--0000 0000--0000 0101 (5 in-memory storage)
-----------------------------------------------------------------------------------
1111 1111--1111 1111--1111 1111--1111 0111 (-9 in-memory storage)

-9 | 5 of the results are-9.

Bitwise OR operations can be used to place some position 1, or to retain certain bits. For example, to put N high 16 bit 1, keep the lower 16 bits, you can do n | The 0xffff0000 operation (0xffff0000 is stored in memory in the form of 1111 1111-1111 1111-0000 0000-0000 0000).

"Instance" to verify the analysis above.

#include <stdio.h>
int main () {
  int n = 0x2d;
  printf ("%d,%d,%x\n", 9 | 5,-9 | 5, n | 0xffff0000);
  return 0;
}

Run Result:

9, ffff002d
Bitwise XOR or operation (^)

Participation in ^ operations two bits not at the same time, the result is 1, the same time result is 0. For example, 0^1 is 1,0^0 for 0,1^1 0.

For example, 9 | 5 can be converted into the following operations:

0000 0000--0000 0000--0000 0000--0000 1001 (9 in-memory storage)
^ 0000 0000--0000 0000--0000 0000--0000 0101 (5 in-memory storage)
-----------------------------------------------------------------------------------
0000 0000--0000 0000--0000 0000--0000 1100 (12 in-memory storage)

9 | The result of 5 is 12.

Another example,-9 | 5 can be converted into the following operations:

1111 1111--1111 1111--1111 1111--1111 0111 (-9 in-memory storage)
^ 0000 0000--0000 0000--0000 0000--0000 0101 (5 in-memory storage)
-----------------------------------------------------------------------------------
1111 1111--1111 1111--1111 1111--1111 0010 (-1 in-memory storage)

-9 | 5 of the results are-14.

Bitwise XOR can be used to invert certain bits. For example, to reverse the high 16 bits of n and keep 16 bits low, you can perform n ^ 0xffff0000 operations (0xffff0000 in memory in the form of 1111 1111-1111 1111-0000 0000-0000 0000).

"Instance" to verify the analysis above.

#include <stdio.h>
int main () {
  unsigned n = 0x0a07002d;
  printf ("%d,%d,%x\n", 9 ^ 5,-9 ^ 5, n ^ 0xffff0000);
  return 0;
}

Run Result:

-14, f5f8002d

Take back operation (~)

Take the inverse operator ~ is the monocular operator, the right binding, the function is to take part in the operation of the bits reverse. For example, ~ is 0,~0 1, which is very similar to the logic in the logical operation.

For example, ~9 can be converted to the following operations:

~ 0000 0000--0000 0000--0000 0000--0000 1001 (9 in-memory storage)
-----------------------------------------------------------------------------------
1111 1111--1111 1111--1111 1111--1111 0110 (-10 in-memory storage)

So the result of ~9 is-10.

For example, ~-9 can be converted to the following operations:

~ 1111 1111--1111 1111--1111 1111--1111 0111 (-9 in-memory storage)
-----------------------------------------------------------------------------------
0000 0000--0000 0000--0000 0000--0000 1000 (9 in-memory storage)

So the result of ~-9 is 8.

"Instance" to verify the analysis above.

#include <stdio.h>
int main () {
  printf ("%d,%d\n", ~9, ~-9);
  return 0;
}

Run Result:

-10, 8

Left-shift Operations (<<)

The left-shift operator << is used to move all bits of the operand to the left several digits, the high drop, the low fill 0.

For example, 9<<3 can be converted to the following operations:

<< 0000 0000--0000 0000--0000 0000--0000 1001 (9 in-memory storage)
-----------------------------------------------------------------------------------
0000 0000--0000 0000--0000 0000--0100 1000 (72 in-memory storage)

So the result of 9<<3 is 72.

Also, ( -9) <<3 can be converted to the following operations:

<< 1111 1111--1111 1111--1111 1111--1111 0111 (-9 in-memory storage)
-----------------------------------------------------------------------------------
1111 1111--1111 1111--1111 1111--1011 1000 (-72 in-memory storage)

So ( -9) The result of <<3 is-72

If the data is small and the dropped high does not contain 1, then the left-shifted n-bit is multiplied by the N-squared of 2.

"Instance" to verify the results above.

#include <stdio.h>

int main () {
  printf ("%d,%d\n", 9<<3, ( -9) <<3);
  return 0;
}

Run Result:

72,-72

Right shift operation (>>)

The right shift operator >> is used to move all bits of the operand to the right several digits, low drop, high up 0 or 1. If the top digit of the data is 0, then fill 0, or 1 if the maximum is 1.

For example, 9>>3 can be converted to the following operations:

>> 0000 0000--0000 0000--0000 0000--0000 1001 (9 in-memory storage)
-----------------------------------------------------------------------------------
0000 0000--0000 0000--0000 0000--0000 0001 (1 in-memory storage)

So the result of 9>>3 is 1.

Also, ( -9) >>3 can be converted to the following operations:

>> 1111 1111--1111 1111--1111 1111--1111 0111 (-9 in-memory storage)
-----------------------------------------------------------------------------------
1111 1111--1111 1111--1111 1111--1111 1110 (-2 in-memory storage)

So ( -9) The result of >>3 is-2

If the dropped low does not contain 1, then the right n position is equal to the N-second party divided by 2 (but the removed bits often contain 1).

"Instance" to verify the results above.

#include <stdio.h>
int main () {
  printf ("%d,%d\n", 9>>3, ( -9) >>3);
  return 0;
}

Run Result:

1,-2

The above is the C language bit operation of knowledge collation, follow-up continue to supplement the relevant information, thank you for your support!

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.