1

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; ?

3
  • Is this an int 0b0011? Commented Feb 7, 2013 at 14:26
  • Do you always know the number of bits in the binary representation or is it arbitrary? Commented Feb 7, 2013 at 14:26
  • Noor: I can define the variable as 'int' if it is necessary. @Jordan Kaye: I just need to output binary values from a GPIO, I need to know the integer value as binaries. So I can say it is not arbitrary Commented Feb 7, 2013 at 14:31

1 Answer 1

4

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.