当前位置:编程学习 > C/C++ >>

将integer的bit位翻转

[cpp] 
#include <iostream> 
typedef unsigned int uint; 
uint switch_digit(uint i, uint j, uint n) 

    uint i_bit = ((n >> i) & 1); 
    uint j_bit = ((n >> j) & 1); 
    if (i_bit ^ j_bit) // should be swithed 
    { 
        n = (1 << j) ^ (1 << i) ^ n; 
    } 
 
    return n; 

 
uint reverse_integer(uint n) 

    uint length = sizeof(uint) * 8; 
 
    uint low = 0; 
    uint high = length - 1; 
    while (low < high) 
    { 
        n = switch_digit(low++, high--, n); 
    } 
    return n; 

 
int main() 
{  www.zzzyk.com
    uint n = 1; 
    n = reverse_integer(n); 
    return 0; 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,