472,978 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,978 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 1777
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
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...
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...
by: Blaxer | last post by:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be... function Calculate_Something(ByVal...
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
by: rls03 | last post by:
I have the following which creates a variable containing a relative path where <xsl:value-of select="."/returns a portion of the filename: <xsl:variable...
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...
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
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...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.