0

I am fairly new to C programming and I am sorry, if the explanation of the problem is unsatisfactory. I have a 32 bit binary number as follows:

typedef unsigned long U32;
U32 a;
a = 11111111000000001111111100000000;

How can I convert that into 4 times 8 bit binary numbers. I just want to take the first 8 bits under a variable and seconds 8 bits into another variable and etc. For example,

typedef uint8;
uint8 b, c, d, e;
b = 11111111;
c = 00000000;
d = 11111111;
e = 00000000;

1 Answers1

0

You could just apply the correct shift and mask to each variable as per:

typedef unsigned long U32;
U32 a;
a = 0xFF00FF00; //11111111000000001111111100000000;

uint8 b, c, d, e;

e = (uint8)(a & 0xFF);
d = (uint8)((a >> 8) & 0xFF);
c = (uint8)((a >> 16) & 0xFF);
b = (uint8)((a >> 24) & 0xFF);

That is the quick and dirty way. You could also use a loop to be more elegant.

progLearner
  • 123
  • 10