Nibble as variable..

C

Chris Dollin

krisworld said:
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`.
 
K

krisworld

hi
is this possible to create a variable of 4 bit in C language. An
Example shall help me a lot.
Thanks
Kris...
 
M

Martin Ambuhl

krisworld said:
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.
 
B

Bart van Ingen Schenau

krisworld said:
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
 
U

user923005

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.
 
E

Eric Sosman

user923005 wrote On 05/22/07 14:46,:
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 */
};
 
K

Keith Thompson

Chris Dollin said:
No. Not of /only/ four bits.

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


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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top