简体   繁体   中英

Binary int to binary char

I am have two variables:

int binary[8];
char array;

where binary variable have only '0s' and '1s' ... So, I want doing that follow

for (i=0;i<8;i++)
    if(binary[i])
        "Here I want put 1 in the position bit of the variable array"
    else
        "Here I want put 0 in the position bit of the variable array"

How I will be able to do this?

If I understand you right, something like this should work...

int binary[8];
char array = 0;

int n = 8;
while (n) {
  array |= binary[--n];
  array <<= 1;
}

This should work in any C99-compliant environment where CHAR_BIT is at least 8 .

for (int i = 0; i < 8; i++)
    array |= binary[i] << i;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM