Thread: bits & bytes

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    bits & bytes

    hi,

    i have a question regarding a struct definition.
    I have a byte data, segemtented like this:

    Code:
    	typedef struct 
    
    {
    	char ID;      // 1 BIT (0)
    	char Mode;	//3 BIt (1-3)
    	char State;	// 4 BIT (4-7)
    	char time;	//16 BIT (8-23)
    } status_messages;


    Question 1:
    Is char the correct definition for a bit value ? How do I define a 3 or 4 bit value?

    Question 2:
    When I want to send this data I need to have the hex value of the first byte (Bit 0 to 7), second (8-15) and third (16-23) byte.
    What could be a smoth way to get this hex values?



    Thanks a lot !

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I'm pretty sure a char is 1 byte (8 bits). If you wanted to declare say 4 bytes:

    Code:
    char someBytes[4];
    No idea how you declare single bits in C.

    Not sure about the hex question sorry.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Code:
    typedef struct 
    {
    	unsigned  ID:1 ;      // 1 BIT (0)
    	unsigned  Mode:3;	//3 BIt (1-3)
    	unsigned State:4;	// 4 BIT (4-7)
    	unsigned time:16;	//16 BIT (8-23)
    } status_messages;
    Here is the correct definition of the 24 bits. On 32-bit computers, there will be 8 bits undefined (unused) to round out the size of an unsigned integer.

    2. Send it how? To what?
    Last edited by Ancient Dragon; 10-11-2005 at 03:45 AM.

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    char is indeed a byte, not necessarily 8 bits, but inevitably 8 bits on all modern machines.

    For individual bits, you can use bitfields, see http://publications.gbdirect.co.uk/c...bitfields.html. Note, I haven't verified that url for accuracy, I just googled for bitfields. The problem with using bitfields, though, is that the implementation defines the ordering.

    Your alternative would be to store the whole thing in a long int (which is at least 32 bits), and use bitwise operators to mask and set each portion appropriately.

    As for converting to hex, look up sprintf.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    Thanks, for all the answers. This definitions looks like what I need:

    Code:
    typedef struct 
    {
    	unsigned  ID:1 ;      // 1 BIT (0)
    	unsigned  Mode:3;	//3 BIt (1-3)
    	unsigned State:4;	// 4 BIT (4-7)
    	unsigned time:16;	//16 BIT (8-23)
    } status_messages;
    But later I need to construct bit sequences for example:

    data[0] = 8 bit = x x x (Mode) x x x (State) x x (LSB and MSB of Time)

    How is this possible?

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    You need to use a combination of bitwise operators including shifting.

    See http://www.phim.unibe.ch/comp_doc/c_...T/bitwise.html and other google results for C bitwise operators.

    As for the bitwise sequence construction, please me more clear on what you want. You've said data[0] is 8 bits, but then shown mode (3 bits), state (4 bits), lsb and msb of time. By lsb and msb do you mean least significant bit or byte? regardless, it won't add up to 8 bits.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    It's true, in this line

    data[0] = 8 bit = x x x (Mode) x x x (State) x x (LSB and MSB of Time)

    I come up with 9 bits. I will avoid bitoperations by using a union:

    Code:
    typedef union
    {
        struct {
    	unsigned  ID:1 ;      // 1 BIT (0)
    	unsigned  Mode:3;	//3 BIt (1-3)
    	unsigned State:4;	// 4 BIT (4-7)
    	unsigned time:16;	//16 BIT (8-23)
                  } stucture;
           unsigned char bytes [3];
    } status_messages;
    But I have no idea how to access the LSB / MSB (LAst significant bit) of for instance the time

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I come up with 9 bits. I will avoid bitoperations by using a union:
    This is non-portable
    1. There is nothing which says that the size of all your bit-fields should be the same as the size of your char array.
    2. There is nothing which says that the lsb of bytes[0] would correspond to say ID.

    If you're looking to pack bits into a byte stream, then you have to do it yourself, a union and bit-fields isn't going to work portably.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The bit operations aren't difficult. Here's an example:
    Code:
    itsme@itsme:~/C$ cat bitfield.c
    #include <stdio.h>
    #include <stdint.h>
    
    int main(void)
    {
      uint32_t bitfield;
    
      bitfield = 1;            // Set the ID, 1 bit
      bitfield |= 5 << 1;      // Set the Mode, 3 bits
      bitfield |= 12 << 4;     // Set the State, 4 bits
      bitfield |= 12345 << 8;  // Set the Time, 16 bits
    
      printf("ID: %d, Mode: %d, State: %d, Time: %d\n",
        bitfield & 0x1, (bitfield >> 1) & 0x7,
        (bitfield >> 4) & 0xF, (bitfield >> 8) & 0xFFFF);
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ ./bitfield
    ID: 1, Mode: 5, State: 12, Time: 12345
    itsme@itsme:~/C$
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM
  5. Bits & Bytes
    By C of Green in forum C++ Programming
    Replies: 8
    Last Post: 06-21-2002, 06:50 PM