Play with those binary stuff ~

Source: Internet
Author: User

There is a child (don't mind ~) After posting this question, a large company performs a binary inverse operation on the unsigned int value, that is, the highest bit and the lowest Bit are exchanged, for example, 7 (0000 0111) Outputs 224 (1110 0000 )?

For the original article, click to open the link. I replied on the 49th floor, but I am worried that I will not be able to find it later. So I will repeat it here. Will it not violate relevant regulations?

I really admire Daniel on the 4 th floor, so I also pasted his code. Thank you ~

Unsigned char source = 7; unsigned char target = 0; For (INT op = 0x80; source! = 0; OP/= 2) {(Source & 0x01 )? Target = (target | OP): Target; Source = source> 1 ;}cout <(INT) Target <Endl; // The above LZ does not know whether it is 8-bit or 32-bit.

Cool people are cool people, the code is neat and neat, people look beautiful, machine execution efficiency is also high.

I can't compare my code with the cool, but I 'd like to paste it and comfort myself:

/* Related tips: set the number to zero and replace the value with the value of & A or | Replace the inverse or ^ * // train of thought for this question: shift the target to the left, and shift the original number to the right, unsigned int transfer (unsigned int X) {unsigned int y = 0; unsigned int temp = 0; For (unsigned int I = 1; I <= 128; I <= 1) // set the font length of the machine here. 128 (0x80) is a machine with 8 binary digits {Y = Y <1; // shift left temp = x & 1; // take the nth bit of X if (temp = 1) // if it is 1, click "or" {y = Y | temp ;} X = x> 1; // right shift of the original number} return y; // Note: Y shifts left first and then the lowest one; otherwise, the lowest one is not obtained .}

Call Statement

Cout <"Transfer (7) =" <transfer (7) <Endl; // 224
Cout <"Transfer (224) =" <transfer (224) <Endl; // 7

Actually, this question is in the book. The related information is as follows:
C and pointer, (beauty) by A. reek, translated by Xu Bo, people's post and telecommunications Press, 2007
---------------------- This is p89's 3rd question. The original title is ----------------------
Compile the function unsigned int reverse_bits (unsigned int value );
The return value of this function is the value after the binary bitwise mode of value is changed from left to right. For example, on a 32-bit machine, the value 25 contains the following bits:
0000 0000 0000 0000 0000 0000 0001
The Return Value of the function should be 2 550 136 832, and its binary mode is:
1001 1000 0000 0000 0000 0000 0000
When writing a function, be sure not to make it dependent on the length of the integer value on your machine.
-------------------- The question has been completed ----------------------

---------------------- Answer given by the author ----------------------
The counterpoint technology does not use hard encoding to avoid portability issues. This solution uses a single digit to shift in an unsigned integer to control the cycle for creating an answer.

/*** Order of flipped bits in an unsigned integer */unsigned int reverse_bits (unsigned int value) {unsigned int answer; unsigned int I; answer = 0; /*** if I is not 0, continue. This makes the cycle irrelevant to the machine's word length, thus avoiding the portability problem */for (I = 1; I! = 0; I <= 1) {/*** shifts the answer to the left and leaves space for the next bit. ** if the last bit of the value is 1, answer performs the or operation with 1 ** and then shifts the value right to the next bit */answer <= 1; if (Value & 1) Answer | = 1; value >>=1;} return answer ;}

-------------------- End the answer given by the author ----------------------

Call Statement

Cout <"reverse_bits (25) =" <reverse_bits (25) <Endl; // 2550136832
Cout <"reverse_bits (2550136832) =" <reverse_bits (2550136832) <Endl; // 25

End

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.