The C language integers are in binary reverse order, and the integer after the reverse order is output.

Source: Internet
Author: User

The C language integers are in binary reverse order, and the integer after the reverse order is output.
The source of the problem is a problem we talked about when we had breakfast with our roommates this morning. We sorted an integer in binary order and then output the value in reverse order. We know that the values are stored in binary format in the memory. For example, if we are a 32-bit machine, every 8 bits is a byte, And the int type occupies 4 bits on a 32-bit machine, 32-bit. For example, 2 = 0000 0000 0000 0000 0000 0000 00000 0010 (32-bit) Inverse ^ 2 = 0100 0000 0000 0000 0000 0000 00000 0000 (^ indicates reversal here) how can this operation be performed? First, add some knowledge: 1) a = a <1, which means to shift a to the left, for example, 0010-> 0100 (usually followed by 0) 2) B = B> 1, which means to shift B to the right, for example, 0100-> 0010 (usually the first is 0) 3) B & 1, this indicates the bitwise AND operation. For example, 2 & 1 actually performs the following operations: 0000 0000 0000 0000 0000 0000 00000 = 2 0010 0000 0000 0000 0000 0000 0000 00000 = 1 0001 0000 0000 0000 0000 0000 0000 00000 = 2 & 1 = 0, set all the first 31 bits to 0 and only keep the last bits unchanged. The result is to retrieve the value of the last bits. 4) a & = ~ 1. Do you not need to explain this? Same as 3), ~ It indicates that the value of 0 is set to 1, and the value of 1 is set to 0. (5) a | = 1, which indicates bitwise OR operation (a = a | 1). For example, 2 | 1 is actually performed as follows: 0000 0000 0000 0000 0000 0000 00000 = 2 0010 0000 0000 0000 0000 0000 0000 00000 = 1 0001 0000 0000 0000 0000 0000 0000 00000 = 2 | 1 = 3 OK, let's take a look at the following code ..... current Environment: win7_32bit, vs2010, c ++ copy code 1 # include <stdio. h> 2 3 int main (void) 4 {5 int I = 32, a = 2; // 32-bit 0000 0000 0000 0000 0000 0000 0000 = 2 6 int B = a; // save another 7 8 while (I --) 9 {10 A = a <1; 11 a & = ~ 1 ;//~ 1 = 1111 1111 1111 1111 1111 1111 1111 1110 ensure that the 31st bits are 012 if (B & 1) // 1 = 0000 0000 0000 0000 0000 0000 0000 000113 {14 a | = 1; // make sure that the 31st bits are set to 115} 16 B = B> 1; 17} 18 19 printf ("% d \ n", a); 20 21 return 0; 22} copy the code idea: the general idea is: 1) first make the values of a and B equal; 2) then, each time a bit is taken from the end of B (from 32nd bits to 0th bits, use the I loop control); Note: B = B> 1, B has been moving right, to ensure that the last bit is retrieved each time. 3) At last, append it to the end of. Note: a = a <1, a has been moving left to ensure that after 32 cycles, the first number appended at the end reaches the first place. Bytes -------------------------------------------------------------------------------------------------------

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.