J
Johnathan Doe
I am trying to do some bit fiddling with unsigned chars. Don't ask me
why I am doing this, I don't know. It's just an exercise.
Here's what I am trying to do: I have one unsigned char of a certain
pattern, and four unsigned chars all with the bit pattern 010 00 010.
The first two binary bits leftmost of the first will go in the middle of
the first char with the bit pattern 010 00 010. The second lot of bits
from the first char will go in the middle of the second char with the
bit pattern 010 00 010. And so on for bits 2, 3 and bits 0, 1 of the
first char.
So, here are the chars with the 010 00 010 patterns.
unsigned char finished[4];
finished[0] |= (0x02 << 5);
finished[0] |= 0x02;
.... and so on for finished[1-3].
And now I have a char with whatever in it, say 'H'.
unsigned char ch = 'H';
Now, I want the leftmost two bits in the middle of finished[0]. What I
attempt to do is OR ch with 0xC0, which gets me just the two leftmost
bits. Then shift them 3 places to the right, to get them in the middle
of the 8-bit char. Then OR with 0x18, which gets me just the two middle
bits, which were previously the leftmost two bits before they were
shifted down into the middle:
finished[0] |= (3 >> (0xC0 & ch)) & 0x18;
Now for finished[1], which has the same 010 00 010 bit pattern, and the
next two leftmost bits of ch, which are bits 4 and 5 (counting from 0
from the left).
This attempts to get a mask for just bits 4 and 5 in ch, shifts them one
place down to the right to get them in the middle, then gets those two
middle bits:
finished[1] |= (1 >> (0x30 & ch)) & 0x18;
Now for finished[2]:
finished[2] |= ((0x0C & ch) << 1) & 0x18;
And for finished[3]:
finished[3] |= ((0x03 & ch) << 3) & 0x18;
Problem is I can't make it work. Am I doing something wrong?
Thanks for your help.
Johnathan
why I am doing this, I don't know. It's just an exercise.
Here's what I am trying to do: I have one unsigned char of a certain
pattern, and four unsigned chars all with the bit pattern 010 00 010.
The first two binary bits leftmost of the first will go in the middle of
the first char with the bit pattern 010 00 010. The second lot of bits
from the first char will go in the middle of the second char with the
bit pattern 010 00 010. And so on for bits 2, 3 and bits 0, 1 of the
first char.
So, here are the chars with the 010 00 010 patterns.
unsigned char finished[4];
finished[0] |= (0x02 << 5);
finished[0] |= 0x02;
.... and so on for finished[1-3].
And now I have a char with whatever in it, say 'H'.
unsigned char ch = 'H';
Now, I want the leftmost two bits in the middle of finished[0]. What I
attempt to do is OR ch with 0xC0, which gets me just the two leftmost
bits. Then shift them 3 places to the right, to get them in the middle
of the 8-bit char. Then OR with 0x18, which gets me just the two middle
bits, which were previously the leftmost two bits before they were
shifted down into the middle:
finished[0] |= (3 >> (0xC0 & ch)) & 0x18;
Now for finished[1], which has the same 010 00 010 bit pattern, and the
next two leftmost bits of ch, which are bits 4 and 5 (counting from 0
from the left).
This attempts to get a mask for just bits 4 and 5 in ch, shifts them one
place down to the right to get them in the middle, then gets those two
middle bits:
finished[1] |= (1 >> (0x30 & ch)) & 0x18;
Now for finished[2]:
finished[2] |= ((0x0C & ch) << 1) & 0x18;
And for finished[3]:
finished[3] |= ((0x03 & ch) << 3) & 0x18;
Problem is I can't make it work. Am I doing something wrong?
Thanks for your help.
Johnathan