Nibble as variable..

Discussion in 'C Programming' started by Chris Dollin, May 22, 2007.

  1. Chris Dollin

    Chris Dollin Guest

    krisworld wrote:

    > is this possible to create a variable of 4 bit in C language.


    No. Not of /only/ four bits.

    (There's bitfields, but lore suggests avoiding them except in
    specialised cases.)

    > An Example shall help me a lot.


    Depending on exactly what your actual problem is, you can use
    `char` (possibly `signed` or `unsigned`), which will often
    waste no more than 4 bits, or you can pack multiple values
    into arithmetic variables -- for example, you can fit two
    four-bit fields into a `char`, at least four into an `int`,
    at least eight into a `long`.

    --
    "You've spotted a flaw in my thinking, Trev" Big Al,/The Beiderbeck Connection/

    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
    Chris Dollin, May 22, 2007
    #1
    1. Advertising

  2. Chris Dollin

    krisworld Guest

    hi
    is this possible to create a variable of 4 bit in C language. An
    Example shall help me a lot.
    Thanks
    Kris...
    krisworld, May 22, 2007
    #2
    1. Advertising

  3. krisworld wrote:
    > hi
    > is this possible to create a variable of 4 bit in C language. An
    > Example shall help me a lot.


    The smallest object in C is the char. It must have a size that will
    allow at least the values 0...255 in an unsigned char, which is the
    equivalent of 8 bits. If you want smaller units, they must be part of
    an object of at least the size of a char. This may be done with bit
    fields, which should be covered in any elementary C text.

    Here is an example.

    struct test {
    unsigned a:4, b:5, c:6;
    };
    /* Each of a, b, and c are bit fields of sizes smaller than a char.
    They may be accessed by name */

    int main(void)
    {
    struct test what;
    unsigned glob;
    what.a = 07; /* the four bits of what.a are set to 0111 */
    glob = what.a;
    return 0;
    }

    There are portability issues when the layout of a structure with bit
    fields os important or when such a struct is large enough to cross
    alignment boundaries. You may find that bitwise operators give a more
    robust solution in such cases.
    Martin Ambuhl, May 22, 2007
    #3
  4. krisworld wrote:

    > hi
    > is this possible to create a variable of 4 bit in C language. An


    No. C does not have any type that occupies less that 8 bits of storage.
    The closest that you can come to a 4-bit variable is to have it as a
    bitfield member of a struct. The entire struct, however, will occupy at
    least 8 bits of storage and probably 32.

    > Example shall help me a lot.


    An example of a struct with three nibble-sized bitfields:

    struct foo {
    unsigned int nibble1 : 4;
    signed int nibble2 : 4;
    signed int nibble3 : 4;
    };

    > Thanks
    > Kris...


    Bart v Ingen Schenau
    --
    a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
    c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    c.l.c++ FAQ: http://www.parashift.com/c -faq-lite/
    Bart van Ingen Schenau, May 22, 2007
    #4
  5. Chris Dollin

    user923005 Guest

    On May 22, 6:56 am, krisworld <> wrote:
    > hi
    > is this possible to create a variable of 4 bit in C language. An
    > Example shall help me a lot.
    > Thanks
    > Kris...


    #include <stdio.h>

    typedef struct fourbits {
    unsigned nybble:4;
    } fourbits;

    int main(void)
    {
    fourbits foo;
    foo.nybble = 0xF;
    printf("foo.nybble = %2u (%x)\n", (unsigned) foo.nybble,
    (unsigned) foo.nybble);
    foo.nybble ^= 0xA;
    printf("foo.nybble = %2u (%x)\n", (unsigned) foo.nybble,
    (unsigned) foo.nybble);
    return 0;
    }

    Tragically, this thing cannot be made into an array and it has other
    limitations.
    user923005, May 22, 2007
    #5
  6. Chris Dollin

    Eric Sosman Guest

    user923005 wrote On 05/22/07 14:46,:
    > On May 22, 6:56 am, krisworld <> wrote:
    >
    >>hi
    >>is this possible to create a variable of 4 bit in C language. An
    >>Example shall help me a lot.
    >>Thanks
    >>Kris...

    >
    >
    > #include <stdio.h>
    >
    > typedef struct fourbits {
    > unsigned nybble:4;
    > } fourbits;
    >
    > [...]
    >
    > Tragically, this thing cannot be made into an array and it has other
    > limitations.


    For clarity's sake, there's nothing wrong with making
    arrays of fourbits structs:

    fourbits f[10];
    for (i = 0; i < 10; ++i)
    f.nybble = i;

    What you can't do is make an array of the bit fields
    within a struct:

    struct four_bidden {
    unsigned nybble:4[10]; /* no can do */
    };

    --
    Eric Sosman, May 22, 2007
    #6
  7. Chris Dollin <> writes:
    > krisworld wrote:
    >> is this possible to create a variable of 4 bit in C language.

    >
    > No. Not of /only/ four bits.
    >
    > (There's bitfields, but lore suggests avoiding them except in
    > specialised cases.)
    >
    >> An Example shall help me a lot.

    >
    > Depending on exactly what your actual problem is, you can use
    > `char` (possibly `signed` or `unsigned`), which will often
    > waste no more than 4 bits, or you can pack multiple values
    > into arithmetic variables -- for example, you can fit two
    > four-bit fields into a `char`, at least four into an `int`,
    > at least eight into a `long`.


    To be painfully pedantic, you can fit *at least* two nibbles into a
    char (some systems, mostly embedded, have CHAR_BIT > 8).

    More realistically, if you're going to be using bitwise operators, you
    should use unsigned types. (char may be either signed or unsigned;
    "unsigned char" is guaranteed to be unsigned.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 22, 2007
    #7
    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. Brad Smallridge
    Replies:
    2
    Views:
    2,527
    Brad Smallridge
    Nov 18, 2004
  2. krisworld

    Nibble as variable..

    krisworld, May 22, 2007, in forum: C Programming
    Replies:
    5
    Views:
    311
    Default User
    May 22, 2007
  3. mfglinux
    Replies:
    11
    Views:
    697
    Roberto Bonvallet
    Sep 12, 2007
  4. hari
    Replies:
    11
    Views:
    933
  5. David Filmer
    Replies:
    19
    Views:
    240
    Kevin Collins
    May 21, 2004
Loading...

Share This Page