[英]binary integer to integer array in c
I have an variable unsigned int x = 0b0011
how can I make it to unsigned integer array, like y[0]=0 ; y[1]=0; y[2]=1; y[3]=1;
我有一个变量unsigned int x = 0b0011
如何使它成为无符号整数数组,如y[0]=0 ; y[1]=0; y[2]=1; y[3]=1;
y[0]=0 ; y[1]=0; y[2]=1; y[3]=1;
? ?
Shifting and bitwise operations. 移位和按位操作。
unsigned x = 0b0011; // yap this is a GNU extension, it doesn't always work even with GCC
const size_t intsize = sizeof(x) * CHAR_BIT; // go go indepency-of-sizeof(int)!
unsigned bits[intsize]; // that's why we love constexprs
int i, j;
for (i = intsize - 1, j = 0; i >= 0; i--, j++) { // and the comma operator too
bits[j] = (x >> i) & 0x1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.