encoding / decoding questions

Discussion in 'C Programming' started by Evangelista Sami, Apr 12, 2004.

  1. 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?
    Evangelista Sami, Apr 12, 2004
    #1
    1. Advertising

  2. Evangelista Sami <> spoke thus:

    > 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.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Apr 12, 2004
    #2
    1. Advertising

  3. Evangelista Sami

    Jack Klein Guest

    On 12 Apr 2004 07:25:55 -0700, (Evangelista Sami)
    wrote in comp.lang.c:

    > 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.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Apr 13, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Slade

    Problem encoding/decoding image

    Slade, Jun 25, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,107
    Natty Gur
    Jun 25, 2003
  2. =?Utf-8?B?TWFyaw==?=

    query string encoding/decoding

    =?Utf-8?B?TWFyaw==?=, Mar 3, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    17,180
    T Conti
    Apr 5, 2004
  3. terry
    Replies:
    2
    Views:
    2,423
    terry
    Nov 3, 2003
  4. LarsM
    Replies:
    18
    Views:
    1,145
    Andreas Prilop
    Feb 11, 2005
  5. Sridhar Anupindi
    Replies:
    0
    Views:
    576
    Sridhar Anupindi
    May 25, 2004
Loading...

Share This Page