C language returns integers in binary Reverse Order

Source: Internet
Author: User

C language returns integers in binary Reverse Order

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, I would like to add some knowledge:

1) a = a <1, indicating to move 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, which indicates bitwise AND operation. For example, 2 & 1 actually performs the following operation:

0000 0000 0000 0000 0000 0000 00000 0010 = 2

0000 0000 0000 0000 0000 0000 00000 0001 = 1

0000 0000 0000 0000 0000 0000 00000 = 2 & 1 = 0

In this operation, all the first 31 bits are set to 0, and only the last BITs remain 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 0010 = 2

0000 0000 0000 0000 0000 0000 00000 0001 = 1

0000 0000 0000 0000 0000 0000 00000 = 2 | 1 = 3

Now let's take a look at the following code .....

Current Environment: win7_32bit, vs2010, c ++

1 # include
 
  
2 3 int main (void) 4 {5 int I = 32, a = 2; // 32-bit 0000 0000 0000 0000 0000 0000 0000 0010 = 2 6 int B =; // 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 115} 16 B = B> 1; 17} 18 19 printf ("% d \ n", a); 20 21 return 0; 22}
 

Thought: The overall thought is:

1) First, make the values of a and B equal;

2) then, one digit is retrieved from the tail of B (from 32nd bits to 0th BITs, which is controlled by the I loop). Note: B = B> 1, B is shifted to the 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 -------------------------------------------------------------------------------------------------------

Address: http://www.cnblogs.com/nchar/p/3915668.html

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.