当前位置: 动力学知识库 > 问答 > 编程问答 >

variables - How do you step through an unisigned int?

问题描述:

I am trying to step through an unsigned int and print the 0 and 1 values of each bit in my 32 bit number. So, for example, I am trying to write a code similar to:

typedef unsigned int uint;

int main(){

unit u = 0x21062910;

int N=0;

for(int i=0; i < 32; i++){

N = u[i];

cout << N;

}

I know I can't access the individual bits like this, but hopefully this gives you an idea of what I am trying to accomplish. The desired output would be something like: 0010 0001 0000 0110 0010 1001 0001 0000

网友答案:

Change the third-to-last line to

           N = u & (1 << i); 

That is, altogether:

typedef unsigned int uint;
int main(){
     uint u = 0x21062910;
     uint N=0; 
     for(int i=0; i < 32; i++){
           N = u & (1 << i); 
           cout << N; 
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: