[Study Notes] [C Language] bit operation, learning note operation

Source: Internet
Author: User

[Study Notes] [C Language] bit operation, learning note operation
1. & bitwise AND

1> Functions

The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0.

2> for example, 9 & 5 is actually 1001 & 101 = 1, so 9 & 5 = 1

3> rule

In binary, the Phase 1 is in the original position, and the phase 0 is 0.

2. | by bit or

1> Functions

If one of the two binary numbers is 1, The result bit is 1. Otherwise, the result bit is 0.

2> example: 9 | 5 is actually 1001 | 101 = 1101, So 9 | 5 = 13

3. ^ bitwise OR

1> Functions

When the binary numbers are different (different), the result is 1. Otherwise, the result is 0.

2> for example, 9 ^ 5 is actually 1001 ^ 101 = 1100, So 9 ^ 5 = 12

3> rule

The result of the same integer ^ is 0. For example, 5 ^ 5 = 0

The result of multiple integers ^ is irrelevant to the order. For example, 5 ^ 6 ^ 7 = 5 ^ 7 ^ 6

Therefore, it is concluded that a ^ B ^ a = B

4 .~ Invert

Returns the decimal digits of the binary digits of integer a, and the sign bit is also reversed (0 to 1, 1 to 0)

 

5. <move left

Remove all the binary numbers of integer a from the left n bits, discard the high bits, and add 0 to the low bits. Shifting n places to the left is actually multiplied by the n power of 2

Since the Left shift is to discard the highest bit, and the 0 complement the second bit, the symbol bit is also discarded, and the result value removed from the Left shift may change the positive and negative values.

6.> right shift

Shifts all the binary numbers of integer a to the right n bits to keep the symbol bit unchanged. Shifting n places to the right is actually dividing by the n power of 2

When the number is positive, the sign bit is 0, and the maximum bit is 0.

When it is a negative number, the sign bit is 1, and the maximum bit is 0 or 1, depending on the requirements of the compilation system

 

7. Learn the code

1 # include <stdio. h> 2 3 4 int main () 5 {6/* by bit and & 7 8 10101010000 9 0000010000010 --------------- 11 0000000000012 13 1011101114 1010110115 --------- 16 1010100117 18 100119 010120 ----- 21 000122 */23 24/* 25 by bit or | 26 100127 010128 ----- 29 110130 */31 32 33/* 34 by bit or ^ 35 1. returns 0 if the same value is exclusive or, for example, 9 ^ 936 2. switch 9 ^ 5 ^ 6 = 9 ^ 6 ^ 537 3. returns the original value if any value is different from 0. 9 ^ 0 = 938 4. a ^ B ^ a = a ^ B = 0 ^ B = b39 40 100141 010142 ----- 43 110044 45 100146 100147 ----- 48 0000049 50 010151 000052 ---- 53 010154 55 9 ^ 5 ^ 9 = 9 ^ 9 ^ 5 = 0 ^ 5 = 556 57 a ^ B ^ = b58 */59 // printf ("% d \ n ", 9 ^ 9); 60 61 // printf ("% d \ n", 9 ^ 5); 62 63/* 64 bitwise inversion ~ 65 ~ 0000 0000 0000 0000 0000 0000 0000 100166 1111 1111 1111 1111 1111 1111 1111 */68 // printf ("% d \ n ",~ 9 ); 69 70/* 71 shift left <72 73 0000 0000 0000 0000 0000 0000 0000 000074 00 0000 0000 0000 0000 0000 0000 10010075 76 9 <1-> 9*2 to the power of 1 = = 1877 9 <2-> 9*2 to the power of 2 = 3678 9 <n-> 9*2 to the power of Npower 79 */80 81 // printf ("% d \ n ", 9 <1 ); 82 83/* 84 right shift> 85 0000 0000 0000 0000 0000 0000 0000 000086 000000 0000 0000 0000 0000 0000 0000 1087 111111 1111 1111 1111 1111 10 88 89 8> 1-> 8/2 = 490 8> 2-> 8/2 to the power of 2 = 291 8> n-> 8/2 to the power of Npower 92 */93 94 printf ("% d \ n", 8> 3); 95 96 return 0; 97}
1 # include <stdio. h> 2 3/* 4 Use bitwise OR operator to exchange the values of two variables 5 */6 7 int main () 8 {9 int a = 10; 10 int B = 11; 11 12/* use the third-party variable 13 int temp = a; 14 a = B; 15 B = temp; 16 */17 18/* 19 a = B-; 20 B = B-a; 21 a = B +; 22 */23 24 // a ^ B ^ a = b25 26 // a --> 10 ^ 1127/B --> 1028 a = a ^ B; 29 B = a ^ B; 30 a = a ^ B; 31 32 printf ("a = % d, B = % d \ n", a, B ); 33 34 return 0; 35}
1 # include <stdio. h> 2/* 3 Use bitwise AND & operator to determine the parity of the variable 4 */5 int main () 6 {7/* 8 15: 1111 9 9: 100110 11 14: 111012 10: 101013 */14 int a = 15; 15 16 a & 1 = 1 // odd 17 a & 1 = 0 // even 18 19/* 20 if (a % 2) {21 printf ("odd \ n"); 22} else {23 printf ("even \ n"); 24} */25 26 // a % 2 = 0? Printf ("even \ n"): printf ("odd \ n"); 27 28 // a % 2? Printf ("odd \ n"): printf ("even \ n"); 29 30 31 32 return 0; 33}
1/* 2 Write a function to output the binary form of integers in the memory. 3 */4 5 # include <stdio. h> 6 void printBinary (int number); 7 8 int main () 9 {10/* 11 0000 0000 0000 0000 0000 0000 0000 000012 0000 0000 0000 0000 0000 0000 0000 111113 0000 14 9: 0000 0000 0000 0000 0000 0000 100115-10: 1111 1111 1111 1111 1111 1111 1111 */17 18 19 // printf ("% d \ n ",~ 9); 20 21 22 printBinary (-10); 23 return 0; 24} 25 26 void printBinary (int number) 27 {28 29 // The record is now moved to the nth 30 // (sizeof (number) * 8)-1 = 3131 int temp = (sizeof (number) <3) -1; 32 33 while (temp> = 0) 34 {35 // first move the bit, and then & 1, get the value of the corresponding bit 36 int value = (number> temp) & 1; 37 printf ("% d", value); 38 39 // 40 temp --; 41 42 // 4 bits per output, output a space 43 if (temp + 1) % 4 = 0) 44 {45 printf (""); 46} 47} 48 49 printf ("\ n"); 50}

 

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.