Left and right flip binary number = = "tedious and streamlined

Source: Internet
Author: User

Realize:

00000000000000000000000000011001

10011000000000000000000000000000

This flip

Method One and method two ideas are roughly the same, can method two but very troublesome.

Method One (Simple)

#include <stdio.h> #include <math.h>unsigned int  reverse_bit (unsigned int  Value) {        int i = 0;         unsigned int value ;        unsigned  int num = 0;for  (i = 1; i <= 32; i++) {         Value = value;         int b =  ((value%2) &&1);        num =  num + b* (Pow (2,  (32 - i)));         value= value >> 1;} Return num;} Int main () {        unsigned int number = 25;         printf ("%u",  reverse_bit (number));} Method two (thought simple but write very complex, I do not know how I think out of)
#include <stdio.h> #include <math.h>unsigned int zhishu (Int x, int cishu) { Unsigned int num = pow (2, cishu);return  (x*num);} Int num_sing (Int max) {if  (max % 2 == 0) {return 0;} else{return 1;}} Unsigned int fzsign (unsigned int a) {unsigned int num = 0;int sing  = num_sing (a);int cishu = 0 + sing;unsigned int tmp =  a;unsigned int b = 0;while  (a != sing) {a = a / 2;cishu+ +;} /*printf ("%d\n",  cishu); */int sings = num_sing (TMP); Int tmp2 = cishu;cishu  = cishu - 1;while  (tmp != sings) {b = tmp % 2;tmp  = tmp / 2;num = num + zhishu (B, cishu); cishu--;} num = num + sings;num = num <<  (32&NBSP;-&NBSP;TMP2); return num;} Int main () {unsigned int a = 25;unsigned int num=fzsign (a);p rintf ("%u",  num);}       two like a hodgepodge!!!


This article is from the "Traces" blog, be sure to keep this source http://wpfbcr.blog.51cto.com/10696766/1703666

Left and right flip binary number = = "tedious and streamlined

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.