need help in bit field

This is a discussion on need help in bit field within the C Programming forums, part of the General Programming Boards category; Hi everyone, I have a program with bit field. However, it displays error when I try to compile. Please show ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    37

    need help in bit field

    Hi everyone, I have a program with bit field. However, it displays error when I try to compile. Please show me why it displays error. Thank you so much.

    #include <stdio.h>
    #include <stdlib.h>
    typedef struct
    {
    unsigned b1:1;
    unsigned b2:1;
    unsigned b3:1;
    unsigned b4:1;
    unsigned b5:1;
    unsigned b6:1;
    unsigned b7:1;
    unsigned b8:1;
    } byte_s;

    int main(void) {
    byte_s bfbyte;
    unsigned char a[]={0x11,0xff};

    bfbyte= (byte_s) a[0]; //displays error: conversion to non-scalar type requested

    printf ("%d %d %d %d : %d %d %d %d \n",
    bfbyte.b1, bfbyte.b2, bfbyte.b3, bfbyte.b4,
    bfbyte.b5, bfbyte.b6, bfbyte.b7, bfbyte.b8);


    printf ("%d %d %d %d : %d %d %d %d \n",
    bfbyte.b1, bfbyte.b2, bfbyte.b3, bfbyte.b4,
    bfbyte.b5, bfbyte.b6, bfbyte.b7, bfbyte.b8);


    return EXIT_SUCCESS;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    Guess what you've ignored 11 times now?

    And no, that isn't binary, it's DECIMAL
    << !! Posting Code? Read this First !! >>
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by lovesunset21 View Post
    Hi everyone, I have a program with bit field. However, it displays error when I try to compile. Please show me why it displays error. Thank you so much.
    I take it you are trying to use a struct to extract the bits from a byte?

    You need to use a union, not a struct.
    Also your bitfield definition is wrong.

    Check your docs, try again... and this time please post your code inside the appropriate tags.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Thanks for your quick reply. I am working with memcopy function, but it displays a warning "implicit declaration of function ‘memcopy’". I don't know why

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Have you included "memory.h"?
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    The function is memcpy and it's in string.h.

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Quote Originally Posted by R.Stiltskin View Post
    The function is memcpy and it's in string.h.
    "memory.h" maps to "string.h" ...
    Devoted my life to programming...

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by Sipher
    "memory.h" maps to "string.h" ...
    That may be true, but it is non-standard. Furthermore, unless it is guaranteed that that header will always include <string.h>, you should also include <string.h> for memcpy.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Oh, ok. I didn't know that.
    Devoted my life to programming...

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Thanks a lot. I have solved my problem. However, I still have question, please help me.

    I am working with bit field. I have

    Code:
    typedef struct bytes{
      unsigned char b8:1;
      unsigned char b7:1;
      unsigned char b6:1;
      unsigned char b5:1;
      unsigned char b4:1;
      unsigned char b3:1;
      unsigned char b2:1;
      unsigned char b1:1;
    } bytes;
    Then I have an array of bytes

    bytes s[10];

    Then I want to copy 0x00 to s[0]. I did as followed:

    Code:
    unsigned char *m;
    *m= 0x00
    s[0]= *((bytes*)(m));
    I don't know why it displays segmentation fault. Please help me. Thank you so much.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by lovesunset21 View Post
    Thanks for your quick reply. I am working with memcopy function, but it displays a warning "implicit declaration of function ‘memcopy’". I don't know why
    Why would you need memcpy?

    First you've got to straighten out your bitfield definition...
    Code:
    typedef struct
    {
    unsigned b1:1;
    unsigned b2:1;
    unsigned b3:1;
    unsigned b4:1;
    unsigned b5:1;
    unsigned b6:1;
    unsigned b7:1;
    unsigned b8:1;
    } byte_s;
    Your struct is going to be 32 bytes long.

    Try it like this...
    Code:
    typedef struct t_byte_s
    { unsigned char b0:1, 
                    b1:1,
                    b2:1,
                    b3:1,
                    b4:1,
                    b5:1,
                    b6:1,
                    b7:1; } 
           byte_s;
    This will be only 1 byte long (although not all compilers will do this for a byte)

    Now you need some means of putting a value in there in one form and getting it out in another. For that we have to use a union...

    Code:
    typedef union t_byte_s
    { unsigned char MyByte;
       unsigned char b0:1, 
                    b1:1,
                    b2:1,
                    b3:1,
                    b4:1,
                    b5:1,
                    b6:1,
                    b7:1; } 
           byte_s;
    Ok, now you can do this...
    Code:
    byte_s testvar;
    
    testvar.MyByte = 127;
    
    if (testvar.b6)
      puts ("eyup, it's a 1");
    
    testvar.MyByte = 0; // clear the bits
    
    testvar.b2 = 1;
    printf("We got %d",testvar.MyByte);  // = 4
    You can feed in individual bits and read the byte or vice versa, hand it a byte and read the bits.
    The reason this works is that in a union all variables occupy the same space (i.e. start at the same address).
    Last edited by CommonTater; 10-30-2010 at 02:28 PM.

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Thanks so much. But i don't really understand.

    Code:
    typedef struct
    {
    unsigned b1:1;
    unsigned b2:1;
    unsigned b3:1;
    unsigned b4:1;
    unsigned b5:1;
    unsigned b6:1;
    unsigned b7:1;
    unsigned b8:1;
    } byte_s;
    Your struct is going to be 32 bytes long.
    I thought that it should be 8 bits long.

    And in case I want to copy its value to a element of an array. What should I do? I face error segmentation fault

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by lovesunset21 View Post
    Thanks so much. But i don't really understand.

    Code:
    typedef struct
    {
    unsigned b1:1;
    unsigned b2:1;
    unsigned b3:1;
    unsigned b4:1;
    unsigned b5:1;
    unsigned b6:1;
    unsigned b7:1;
    unsigned b8:1;
    } byte_s;
    Your struct is going to be 32 bytes long.
    I thought that it should be 8 bits long.

    And in case I want to copy its value to a element of an array. What should I do? I face error segmentation fault
    You are using "unsigned" which is an int (32bits, 4 bytes on most systems)
    See the semicolon at the end of each line?
    That causes each bit to occupy a separate 4 byte value... 4 * 8 = 32 bytes.

    Note that I specified unsigned char ... on most systems that is 8bits.
    By terminating each line with a coma, except the last one, I've told it to put all 8 values in the same unsigned char... thus, only 1 byte.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by lovesunset21 View Post
    Thanks so much. But i don't really understand.

    Code:
    typedef struct
    {
    unsigned b1:1;
    unsigned b2:1;
    unsigned b3:1;
    unsigned b4:1;
    unsigned b5:1;
    unsigned b6:1;
    unsigned b7:1;
    unsigned b8:1;
    } byte_s;
    Your struct is going to be 32 bytes long.
    I thought that it should be 8 bits long.

    And in case I want to copy its value to a element of an array. What should I do? I face error segmentation fault
    Take a look at the way I showed you to do it....

    If you want to copy each bit to an array element you will have to do it manually...

    Code:
    Int bitarray[8];  // array for bits
    
    testvar.MyByte = x;  // stuff in a value
    
    // copy out the bits
    bitarray[0] = testvar.b0;
    bitarray[1] = testvar.b1;
    ...
    bitarray[7] = testvar.b7;
    
    // or to get them as ascii 1 and 0 for printing
    char txtarray[8];
    
    testvar.MyByte = x; // value in
    // ascii out
    txtarray[0] = testvar.b0 + '0';
    ...
    txtarray[7] = textvar.b7 + '0';
    
    //or to be extra sneaky
    sprintf(txtarray,"%d%d%d%d%d%d%d%d",
                 testvar.b0,testvar.b1,testvar.b2,testvar.b3,testvar.b4,testvar.b5,testvar.b6,testvar.b7);
    Last edited by CommonTater; 10-30-2010 at 02:59 PM.

  15. #15
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Thanks so much. Can you explain the UNION keyword for me?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit level permutation function
    By zxcv in forum C Programming
    Replies: 2
    Last Post: 07-27-2008, 01:26 PM
  2. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 07:02 PM
  3. Replies: 7
    Last Post: 12-10-2004, 07:18 AM
  4. BIT field confusing
    By C-Dumbie in forum C Programming
    Replies: 4
    Last Post: 09-14-2002, 02:33 AM
  5. linked lists problem
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-17-2002, 10:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21