Unsigned integer binary

Source: Internet
Author: User

From: http://blog.kingsamchen.com/archives/647

The problem I encountered when I checked pointers on C two days ago is described as follows:

Write a function that accepts an unsigned integer (unsigned INT) and places the integer in the binary (that is, the reverse bit ).

For example, the binary digit of the decimal integer 7 is 0000 0000 0000 0111, and the result of the reverse decimal integer 7 is 1110 0000 0000 0000, Which is 3758096384.

The function should return the reverse number.

It should be noted that, in order to maintain portability, It is not assumed that the int type is 32-bit long, so the function to be written must be effective for both 16-bit and 32-bit.

The key to solving the problem is how to correctly process the length of different bytes.

In fact, we do not need to know the specific length of bytes. We only need to know when the operation should be stopped.

Lenovo bitwise-related content. Here we use an unsigned int flag I, initialize it as 1, and perform the Left shift operation on variable I.

When the I value is 0, it indicates that the operation is left out of the bounds, and the operation should be stopped.

The demo is as follows:

[code=c/c++]typedef unsigned int uint;// reverses value on bit baseduint ReverseBit(uint value){    uint answer = 0U;    for (uint i = 1U; i != 0; i <<= 1)    {        answer <<= 1;        if (value & 1)        {            answer |= 1;        }        value >>= 1;    }    return answer;}[/code]

Each time we check the first digit of the value, the corresponding answer bit is set based on the result.

Before each operation, answer needs to move one digit to the right to reserve space for the next operation. There are two reasons for doing this:

First, after the right shift, the previous bit value will rise to a high level, just catering to our reverse goal

Second, after each operation, answer cannot perform the displacement operation. Otherwise, the displacement after the last operation will make the code run incorrectly.

So we need to perform the unit shift before the operation

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.