473,413 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Nibble as variable..

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

May 22 '07 #1
6 11647
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

May 22 '07 #2
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.

May 22 '07 #3
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/
May 22 '07 #4
On May 22, 6:56 am, krisworld <krishnamurthy.i...@gmail.comwrote:
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.

May 22 '07 #5
user923005 wrote On 05/22/07 14:46,:
On May 22, 6:56 am, krisworld <krishnamurthy.i...@gmail.comwrote:
>>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 */
};

--
Er*********@sun.com
May 22 '07 #6
Chris Dollin <ch**********@hp.comwrites:
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) ks***@mib.org <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"
May 22 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Scott | last post by:
I have an XML Document in a format like: <Variable name="Bob">ABCDEFG</Variable> <Variable name="Steve">QWERTYUI</Variable> <Variable name="John">POIUYTR</Variable> <Variable...
4
by: Frederik Sørensen | last post by:
I include a xslt stylesheet with variables for all the error messages in my system. <xsl:variable name="Banner_error_1"> errormessage 1 for banner </xsl:variable> <xsl:variable...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
5
by: krisworld | last post by:
hi is this possible to create a variable of 4 bit in C language. An Example shall help me a lot. Thanks Kris...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
8
by: =?Utf-8?B?UmljYXJkbyBRdWludGFuaWxsYQ==?= | last post by:
i need to convert data from string to nibble wich (nibble is a four bits representation) As example i have the following code string data1 = "12345678"; so ¿how can i convert this data...
1
by: sigamani | last post by:
how is write the code in nibble swapping
9
by: hari | last post by:
hi all, Im writing a code in which the bytes,needed to be splitted in to nibbles.each nibble needs to be made as byte. for example: consider 3B as byte, on splitting this I will get 3 and B.The...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.