Hi
On Sun, 15 Jun 2008 03:00:50 -0700, Anna wrote:
On 15 juin, 11:52, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
>Anna <petitmou...@gm ail.comwrites:
I try to put 8 int bit for example 10100010 into one character of
type char(1 octet) with no hope . Could anyone propose a simple way
to do it? Thank you very much.
It would help is we saw what you did. I would write:
unsigned char c = 0xA2; /* Hex A2 is 1010 0010 */
Not that unsigned char is almost always safer for this sort of thing.
thank you Ben. I have bit sequence 01010101.... that I want to implement
into a simulator, but in the simulator, they use a char as a type of
data since each int comprise between 2-4 octet and one char is only 1
octet (=8 bits 0 or 1). I guess it will make the simulator run faster,
I'm not sure.
Do you mean that you have eight integers but that each one has either the
value 1 or 0? In that case, do:
Est-ce que vous-voulez dire que vous avez huite integers et que chaqun
d'entre eux a le valeur 1 ou 0? En ce cas, faitez:
int integers[8];
unsigned char character;
character= ( integers[0] ? (1 << 0) : 0 )
| ( integers[1] ? (1 << 1) : 0 )
| ( integers[2] ? (1 << 2) : 0 )
| ( integers[3] ? (1 << 3) : 0 )
| ( integers[4] ? (1 << 4) : 0 )
| ( integers[5] ? (1 << 5) : 0 )
| ( integers[6] ? (1 << 6) : 0 )
| ( integers[7] ? (1 << 7) : 0 );
Actually this works if the integers have the value 0 or non-0. If you
are certain that they can only be 1 or 0, the following may be faster:
Ceci marche si les integers ont les valeurs 0 ou non-0. Si vous etez
certain que ils n'ont que 1 ou 0, le ci-dessus peut-etre plus vite:
character= (integers[0] << 0)
| ( integers[1] << 1)
| ( integers[2] << 2)
| ( integers[3] << 3)
| ( integers[4] << 4)
| ( integers[5] << 5)
| ( integers[6] << 6)
| ( integers[7] << 7);
HTH
viza