QQ wrote:
unsigned char a;
the a[0-3] represents a 4-bit binary,
a[4-6] represents another 3-bit binary.
I'd like to convert a[0-3] into a decimal.
For instance if {a[3] a[2] a[1] a[0}}= {0 1 0 1] the decimal should be
5
My program is like
if((a && 0x00)==1) return 0;
else if (a&& 0x01)==1) return 1;
I don't understand how the above can do what you want.
Is there any simpler way for it?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
int main(void)
{
unsigned char a;
int i;
srand(time(0));
for (i = 0; i < 4; i++) {
a = (unsigned) rand();
printf
("a = %3u (%#03o, %#02x), first 4 bits=%u,"
" next 3 bits=%u\n",
a, a, a, a >> (CHAR_BIT - 4), 07 & (a >> (CHAR_BIT - 7)));
}
return 0;
}
a = 186 (0272, 0xba), first 4 bits=11, next 3 bits=5
a = 171 (0253, 0xab), first 4 bits=10, next 3 bits=5
a = 19 (023, 0x13), first 4 bits=1, next 3 bits=1
a = 98 (0142, 0x62), first 4 bits=6, next 3 bits=1