Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Nibble as variable..

Reply
Thread Tools

Nibble as variable..

 
 
Chris Dollin
Guest
Posts: n/a
 
      05-22-2007
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

 
Reply With Quote
 
 
 
 
krisworld
Guest
Posts: n/a
 
      05-22-2007
hi
is this possible to create a variable of 4 bit in C language. An
Example shall help me a lot.
Thanks
Kris...

 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      05-22-2007
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.

 
Reply With Quote
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      05-22-2007
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/
 
Reply With Quote
 
user923005
Guest
Posts: n/a
 
      05-22-2007
On May 22, 6:56 am, krisworld <(E-Mail Removed)> 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.

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      05-22-2007
user923005 wrote On 05/22/07 14:46,:
> On May 22, 6:56 am, krisworld <(E-Mail Removed)> 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[i].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 */
};

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-22-2007
Chris Dollin <(E-Mail Removed)> 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 > .

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) (E-Mail Removed) <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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting byte to nibble and converted nibble to bytes hari C Programming 11 03-21-2008 01:08 AM
Nibble as variable.. krisworld C Programming 5 05-22-2007 10:31 PM



Advertisments