define a data type of 1 bit size

Discussion in 'C Programming' started by Angel Lopez, Sep 7, 2004.

  1. Angel Lopez

    Angel Lopez Guest

    Sorry if this appears twice but my first try didn't seem to make it.
    but this is my first post (hopefully)...
    I have a binary variable (it can contain either a 0 or a 1).
    Is there any way I can define a data type that uses only 1 bit. So
    far I have defined it as a char variable. I've searched everywhere but
    I don't seem to find any place that explains how to define this type
    of data type. The closest thing I've found are bit fields in
    structures, I would like something like bit fields but without the
    structure.
    something like
    unsigned MyVariable :1;
    Thanks,
    Angel
    Angel Lopez, Sep 7, 2004
    #1
    1. Advertising

  2. Angel Lopez wrote:

    > Sorry if this appears twice but my first try didn't seem to make it.
    > but this is my first post (hopefully)...
    > I have a binary variable (it can contain either a 0 or a 1).
    > Is there any way I can define a data type that uses only 1 bit.


    No. The smallest data type is the char, which is guaranteed to be at
    least 8 bits.

    You can use bit-masking on an unsigned integral type (unsigned char,
    unsigned short, unsigned int, unsigned long, unsigned long long) or you
    can use bitfields in a structure if you want.

    > So
    > far I have defined it as a char variable. I've searched everywhere but
    > I don't seem to find any place that explains how to define this type
    > of data type.


    see above.
    Martin Ambuhl, Sep 7, 2004
    #2
    1. Advertising

  3. [snips]

    On Tue, 07 Sep 2004 00:11:19 -0700, Angel Lopez wrote:

    > Sorry if this appears twice but my first try didn't seem to make it.
    > but this is my first post (hopefully)...
    > I have a binary variable (it can contain either a 0 or a 1).
    > Is there any way I can define a data type that uses only 1 bit.


    I seem to recall having worked with a compiler, years ago, that supported
    a "bit" type... but that's totally non-portable, even to other compilers
    on the same platform.

    Probably the easiest is to just use unsigned chars or unsigned ints and a
    function or two; it's not that hard to test to see if a given bit is set,
    or to set or clear it.

    Example: suppose we want to work with, oh, 500 bits, we might do something
    like this:

    unsigned char bitbuff[(500+(CHAR_BIT-1))/CHAR_BIT];

    void setbit( unsigned bit, unsigned state )
    {
    unsigned byte = bit / CHAR_BIT;
    bit %= CHAR_BIT;

    bitbuff[byte] &= ~(1 << bit ); /* turns bit off */
    if ( state )
    bitbuff[byte] |= (1 << bit ); /* turn on if necessary */
    }


    It's morning, and I haven't had my coffee yet, so it may not be spot on,
    but the concept should work.
    Kelsey Bjarnason, Sep 8, 2004
    #3
  4. "Kelsey Bjarnason" <> wrote in message
    news:p...
    > [snips]
    >
    > On Tue, 07 Sep 2004 00:11:19 -0700, Angel Lopez wrote:
    >
    > > Sorry if this appears twice but my first try didn't seem to make it.
    > > but this is my first post (hopefully)...
    > > I have a binary variable (it can contain either a 0 or a 1).
    > > Is there any way I can define a data type that uses only 1 bit.

    >
    > I seem to recall having worked with a compiler, years ago, that supported
    > a "bit" type... but that's totally non-portable, even to other compilers
    > on the same platform.
    >
    > Probably the easiest is to just use unsigned chars or unsigned ints and a
    > function or two; it's not that hard to test to see if a given bit is set,
    > or to set or clear it.
    >
    > Example: suppose we want to work with, oh, 500 bits, we might do something
    > like this:
    >
    > unsigned char bitbuff[(500+(CHAR_BIT-1))/CHAR_BIT];
    >
    > void setbit( unsigned bit, unsigned state )
    > {
    > unsigned byte = bit / CHAR_BIT;
    > bit %= CHAR_BIT;
    >
    > bitbuff[byte] &= ~(1 << bit ); /* turns bit off */
    > if ( state )
    > bitbuff[byte] |= (1 << bit ); /* turn on if necessary */
    > }
    >
    >
    > It's morning, and I haven't had my coffee yet, so it may not be spot on,
    > but the concept should work.
    >
    >


    But is this bit an ANSI c data type? MPJ
    Merrill & Michele, Sep 8, 2004
    #4
  5. Angel Lopez

    c453___ Guest

    if u really want to be economic with process's memory u can use for
    example a char (or int) and store in it up to 8 (or 32) one bit values ;)

    --
    grtz();
    c453___, Sep 10, 2004
    #5
  6. c453___ <> spoke thus:

    > if u really want to be economic with process's memory u can use for
    > example a char (or int) and store in it up to 8 (or 32) one bit values ;)


    You mean CHAR_BIT or sizeof( int )*CHAR_BIT.

    --
    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, Sep 10, 2004
    #6
    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. Replies:
    3
    Views:
    1,742
    Timothy Bendfelt
    Jan 19, 2007
  2. Angel Lopez

    define a data type of 1 bit size

    Angel Lopez, Sep 6, 2004, in forum: C Programming
    Replies:
    21
    Views:
    2,505
    Merrill & Michele
    Sep 10, 2004
  3. Yevgen Muntyan

    #define ALLOCIT(Type) ((Type*) malloc (sizeof (Type)))

    Yevgen Muntyan, Feb 9, 2007, in forum: C Programming
    Replies:
    10
    Views:
    895
    Yevgen Muntyan
    Feb 13, 2007
  4. Replies:
    9
    Views:
    966
    Juha Nieminen
    Aug 22, 2007
  5. akineko
    Replies:
    7
    Views:
    1,323
    akineko
    Dec 22, 2008
Loading...

Share This Page