The C language converts integers in reverse in binary order

Source: Internet
Author: User

The source of the problem, this morning and a roommate to eat breakfast when talking about a problem, an integer in reverse order of binary, and then output the value after the reverse.

We know that the values in memory are stored in binary form, if we are 32-bit machine, every 8 bits is a byte, int type on the 32-bit machine is accounted for 4 bytes, that is 32 bits.

such as 2 = 0000 0000 0000 0000 0000 0000 00000 0010 (32-bit)

Inverse ^2 = 0100 0000 0000 0000 0000 0000 00000 0000 (here with ^ for reversal)

So how does this work? First, add some knowledge:

1) A = a << 1, means to move a to the left one bit, such as: 0010->0100 (generally back is 0)

2) b = b >> 1, means to move B to the right one bit, such as: 0100->0010 (generally preceded by a complement of 0)

3) B & 1, this means bitwise AND operation, such as: 2 & 1, in fact, the following actions are performed:

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 0000 = 2 & 1 = 0

This operation, the first 31 bits are all set to 0, only the last one remains unchanged, the effect is to remove the last value.

4) A &=, does this need to be explained? Same as 3), but ~ indicates the position of the 0, set to 1;1 position, set to 0.

5) A |= 1, which represents a bitwise OR operation (A = a | 1), for example: 2 | 1, the following actions are actually performed:

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 0011 = 2 | 1 = 3

OK, here's a look at the following code .....

Current Environment: win7_32bit,vs2010,c++

1#include <stdio.h>2 3 intMainvoid)4 {5     inti = +, a =2;//32-bit 0000 0000 0000 0000 0000 0000 0000 0010 = 26     intb = A;//Save a copy7 8      while(i--)9     {TenA = a <<1; OneA &= ~1;//to = 1111 1111 1111 1111 1111 1111 1111 1110 Make sure the 31st bit is 0 A         if(B &1)//1 = 0000 0000 0000 0000 0000 0000 0000 0001 -         { -A |=1;//Make sure the 31st bit is 1. the         } -b = b >>1; -     } -  +printf"%d\n", a); -  +     return 0; A}

Thought: The general idea is:

1) First make the value of a, B equal;

2) Then, one at a time from the tail of B (from the 32nd position to the No. 0 position, with I loop control); Note: b = b >> 1,b has been shifted right to ensure that the last one is removed each time.

3) Finally, append it to the end of a. Note: a = a << 1,a has been shifted to the left to ensure that after 32 cycles, the first appended number is reached at the end of the first digit.

-------------------------------------------------------------------------------------------------------

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.