簡體   English   中英

c中的二進制整數到整數數組

[英]binary integer to integer array in c

我有一個變量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;

移位和按位操作。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM