encoding / decoding questions

E

Evangelista Sami

hello

i have to write a program in which i encode / decode datas into bits
vector.
and i do not really know how to do it.
my solution is to represent my bits as a char *.
so extracting the 32 first bits of a vector is done by :
bits[0] << 24 | bits[1] << 16 | bits[2] << 8 | bits[3]
or
bits[0] >> 24 | bits[1] >> 16 | bits[2] >> 8 | bits[3]
depending on how i encode my datas. is this correct?

so the program makes the hypothesis that a char takes 8 bits.
i dont know if this is correct. is this defined in C99?

any help would be very useful. in particuliar is there a more
efficient way to do this?
 
C

Christopher Benson-Manica

Evangelista Sami said:
so the program makes the hypothesis that a char takes 8 bits.
i dont know if this is correct. is this defined in C99?

The macro CHAR_BIT is what you're looking for, although it's usually
8.
 
J

Jack Klein

hello

i have to write a program in which i encode / decode datas into bits
vector.
and i do not really know how to do it.
my solution is to represent my bits as a char *.
so extracting the 32 first bits of a vector is done by :
bits[0] << 24 | bits[1] << 16 | bits[2] << 8 | bits[3]
or
bits[0] >> 24 | bits[1] >> 16 | bits[2] >> 8 | bits[3]
depending on how i encode my datas. is this correct?

so the program makes the hypothesis that a char takes 8 bits.
i dont know if this is correct. is this defined in C99?

any help would be very useful. in particuliar is there a more
efficient way to do this?

As already pointed out, the macro CHAR_BIT defined in <limits.h>
contains the number of bits in objects of the character types. It is
often best to just use 8, since CHAR_BIT must be at least 8, and if
you actually port this code to an architecture where character types
have more than 8 bits, it will still work.

There are a few other things you should do, however. First you should
used "unsigned char", not "signed char" or just plain "char", for the
elements of your array.

Second, you should cast the values to unsigned long when assembling
them. On most platforms, the unsigned chars of your array will
promote to signed ints. If ints have only 16 bits, shifting by 24 and
16 produce undefined behavior. Even if your ints have 32 bits,
shifting signed values can create a trap representation and cause
undefined behavior.

When dealing with bits, always use all unsigned types.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top