Write function return value value of binary mode from left to right after rollover

Source: Internet
Author: User

To write a function:

unsigned int reverse_bit (unsigned int value);

The return value of this function values The value of the BITS mode from left to right.

For example:

On the four-position machine This value includes the following members:

00000000000000000000000000011001

After flipping: (2550136832)

10011000000000000000000000000000

The program results return:

2550136832

The result of the observation output is the value of the binary reverse order of the input number.

The code is as follows:

#include <stdio.h> #include <stdlib.h>unsigned int revease_bit (unsigned int val) {int i;unsigned int ret=0; The two statements in for (i=0;i<32;i++) {Ret<<=1;//for cannot be interchanged ret|= ((val>>i) &1),/*val right-shift I-bit, and "1" take the value of binary each bit with operation, Again and RET or operation, the last shift, so that it in reverse order. You can also use the following notation: (add header file #include<math.h>) ret + = ((num >> i) & 1) *pow (2, 31-i); */}return ret;} int main () {unsigned int val;scanf ("%u", &val);p rintf ("%u\n", Revease_bit (Val)),//%u for unsigned decimal output system ("pause"); return 0;}

This article is from the "Materfer" blog, make sure to keep this source http://10741357.blog.51cto.com/10731357/1708429

Write function return value value of binary mode from left to right after rollover

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.