471,063 Members | 5,465 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

bit variable

sorry for the dumb question but how do i code a bit
variable in c?

i mean - shoudlnt the synax need to be:
bit j;
j = 1;

correct me if im wrong (and i suppose i am).
Nov 17 '05 #1
2 1689
Use a bitfield variable. Be careful if you try to map the bitfield's byte to
an actual hardware / memory location like a UART register, since bitfields
are platform dependant. Some realtime systems do define bits in the C
compiler that support them, but this is not standard.

Here's some details from MSDN:
C Bit Fields
In addition to declarators for members of a structure or union, a structure
declarator can also be a specified number of bits, called a "bit field." Its
length is set off from the declarator for the field name by a colon. A bit
field is interpreted as an integral type.

struct-declarator :
type-specifier declarator opt : constant-expression
The constant-expression specifies the width of the field in bits. The
type-specifier for the declarator must be unsigned int, signed int, or int,
and the constant-expression must be a nonnegative integer value. If the
value is zero, the declaration has no declarator. Arrays of bit fields,
pointers to bit fields, and functions returning bit fields are not allowed.
The optional declarator names the bit field. Bit fields can only be declared
as part of a structure. The address-of operator (&) cannot be applied to
bit-field components.

Unnamed bit fields cannot be referenced, and their contents at run time are
unpredictable. They can be used as "dummy" fields, for alignment purposes.
An unnamed bit field whose width is specified as 0 guarantees that storage
for the member following it in the struct-declaration-list begins on an int

Bit fields must also be long enough to contain the bit pattern. For example,
these two statements are not legal:

short a:17; /* Illegal! */
int long y:33; /* Illegal! */
This example defines a two-dimensional array of structures named screen.

unsigned short icon : 8;
unsigned short color : 4;
unsigned short underline : 1;
unsigned short blink : 1;
} screen[25][80];
The array contains 2,000 elements. Each element is an individual structure
containing four bit-field members: icon, color, underline, and blink. The
size of each structure is two bytes.

Bit fields have the same semantics as the integer type. This means a bit
field is used in expressions in exactly the same way as a variable of the
same base type would be used, regardless of how many bits are in the bit

"Avi Laviad" <av********@hotmail.com> wrote in message
sorry for the dumb question but how do i code a bit
variable in c?

i mean - shoudlnt the synax need to be:
bit j;
j = 1;

correct me if im wrong (and i suppose i am).

Nov 17 '05 #2
if you really just need a single bit, then you can use bool. if you need
many bits, use a bitfield or encode the bits in an integral value like
unsigned int.


[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Frederik Sørensen | last post: by
134 posts views Thread by James A. Donald | last post: by
23 posts views Thread by Russ Chinoy | last post: by
3 posts views Thread by rls03 | last post: by
1 post views Thread by pbmods | last post: by
2 posts views Thread by Florian Loitsch | last post: by
37 posts views Thread by minkoo.seo | last post: by
112 posts views Thread by istillshine | last post: by

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.