473,698 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

define a data type of 1 bit size

Sorry if I am too naive, but this is my first post...
I have a binary variable (it can contain either a 0 or a 1).
Is there any way I can define a data type that uses only 1 bit. So
far I have defined it as a char variable. I've searched everywhere but
I don't seem to find any place that explains how to define this type
of data type. The closest thing I've found are bit fields in
structures, I would like something like bit fields but without the
structure.
something like
unsigned MyVariable :1;
Thanks,
Angel
Nov 14 '05 #1
21 8711
"Angel Lopez" <an*******@hotm ail.com> wrote in message
news:9d******** *************** ***@posting.goo gle.com...
Sorry if I am too naive, but this is my first post...
I have a binary variable (it can contain either a 0 or a 1).
Is there any way I can define a data type that uses only 1 bit. So
far I have defined it as a char variable. I've searched everywhere but
I don't seem to find any place that explains how to define this type
of data type. The closest thing I've found are bit fields in
structures, I would like something like bit fields but without the
structure.


If you don't have too many of them, using a built-in type such as char or
even int is fine - just use zero for 0 and non-zero for 1.

If you want lots (and lots) of them in an array, you can save memory and
perhaps get better performance by using an unsigned type such as unsigned
int and writing simple functions or macros to access them by specifying a
bit index. The optimal type will depend on the platform, but it's easy to
write code so you can change the type to experiment if and when you find
performance to be a problem.

Compilers for some microcontroller s (such as 8051-alikes) have extensions to
the language that allow you to define bit variables. Bit variables are
typically more useful in these embedded environments where memory may be
very limited.

Alex
Nov 14 '05 #2
Angel Lopez wrote:
Sorry if I am too naive, but this is my first post...
I have a binary variable (it can contain either a 0 or a 1).
Is there any way I can define a data type that uses only 1 bit. So
far I have defined it as a char variable. I've searched everywhere but
I don't seem to find any place that explains how to define this type
of data type. The closest thing I've found are bit fields in
structures, I would like something like bit fields but without the
structure.
something like
unsigned MyVariable :1;
Thanks,
Angel

In C99 you can write:
#include <stdbool.h>

bool myvar = 1;

This will take a char (8 bits). You can't address bits so this will be
the same in all compilers. The advantage is that if you write
myvar=78;
printf("%d\n",m yvar);
that will print 1 and not 78.

jacob
Nov 14 '05 #3
MVC++6 allows a boolean type which will port almost nowhere and takes up a
byte anyways. Although I've never laid eyes on ANSI, I thought the deal was
that bytes always have eight bits and all data types are a multiple of
bytes. You could certainly write a program to squeeze eight ones or zeros
into a byte, but I think it's a stretch to call what results a proper data
type. MPJ
"Angel Lopez" <an*******@hotm ail.com> wrote in message
news:9d******** *************** ***@posting.goo gle.com...
Sorry if I am too naive, but this is my first post...
I have a binary variable (it can contain either a 0 or a 1).
Is there any way I can define a data type that uses only 1 bit. So
far I have defined it as a char variable. I've searched everywhere but
I don't seem to find any place that explains how to define this type
of data type. The closest thing I've found are bit fields in
structures, I would like something like bit fields but without the
structure.
something like
unsigned MyVariable :1;
Thanks,
Angel

Nov 14 '05 #4
On Mon, 06 Sep 2004 16:50:46 +0200, jacob navia
<ja***@jacob.re mcomp.fr> wrote in comp.lang.c:
Angel Lopez wrote:
Sorry if I am too naive, but this is my first post...
I have a binary variable (it can contain either a 0 or a 1).
Is there any way I can define a data type that uses only 1 bit. So
far I have defined it as a char variable. I've searched everywhere but
I don't seem to find any place that explains how to define this type
of data type. The closest thing I've found are bit fields in
structures, I would like something like bit fields but without the
structure.
something like
unsigned MyVariable :1;
Thanks,
Angel In C99 you can write:
#include <stdbool.h>

bool myvar = 1;

This will take a char (8 bits). You can't address bits so this will be
the same in all compilers. The advantage is that if you write


No, this will be at least sizeof(char), which is 8 bits on most
platforms but larger on others. And there are some implementations
that use (un)signed ints for _Bool.

What is the same on all compilers is that you cannot have any objects
smaller than one byte in size, however many bits a byte may contain.
myvar=78;
printf("%d\n",m yvar);
that will print 1 and not 78.

jacob


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #5
On 6 Sep 2004 06:35:19 -0700, an*******@hotma il.com (Angel Lopez)
wrote in comp.lang.c:
Sorry if I am too naive, but this is my first post...
I have a binary variable (it can contain either a 0 or a 1).
Is there any way I can define a data type that uses only 1 bit. So
far I have defined it as a char variable. I've searched everywhere but
I don't seem to find any place that explains how to define this type
of data type. The closest thing I've found are bit fields in
structures, I would like something like bit fields but without the
structure.
something like
unsigned MyVariable :1;
Thanks,
Angel


This just can't be done in C. Bit-fields are only allowed inside
structures, and other than bit-fields, no object is allowed to be
smaller than sizeof(char).

If you actually have a large enough number of these values that memory
space becomes important, the FAQ for this group has an example of
packing multiple bits into larger data types.

See http://www.eskimo.com/~scs/C-faq/q20.8.html

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #6
On Mon, 6 Sep 2004 10:32:09 -0500, "Merrill & Michele"
<be********@com cast.net> wrote in comp.lang.c:

First, don't top-post. Material you add in a reply belongs after
quoted material you are commenting on. If you don't want to get a
real newsreader, use Google to search for a patch to Outlook Express
that defaults the entry point in replies to the proper location.

MVC++6 allows a boolean type which will port almost nowhere and takes up a
byte anyways. Although I've never laid eyes on ANSI, I thought the deal was
that bytes always have eight bits and all data types are a multiple of
bytes. You could certainly write a program to squeeze eight ones or zeros
into a byte, but I think it's a stretch to call what results a proper data
type. MPJ


You thought wrong. A byte in C contains CHAR_BIT bits, this macro
defined in <limits.h>. It must be at least 8, but may be more and is
16 or 32 on some implementations .

You were right about the fact that all objects must be a multiple, 1
or more, of sizeof(char).

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #7
"Merrill & Michele" <be********@com cast.net> wrote in message news:<Lq******* *************@c omcast.com>...
Although I've never laid eyes on ANSI, I thought the deal was
that bytes always have eight bits and all data types are a multiple of
bytes.


ANSI guarantees that "char" has _at least_ eight bits. Moreover, it
guarantees that a "char *" can access _any_ bit in memory[1].

Bear with me.

For example, on the 80x86 architecture memory is divided in the eight
bit chunks we've all come to know and love as "bytes". So "char" does
the sensible thing and defaults to eight bits also [2].

Now, let's take the PDP-10. This strange beast has it's memory divided
into 36 bits words. If "char" would've been eight bits, we wouldn't be
able to access (36 % 8) = 4 bits per word, which is _bad_.
So, as a work-around, "char" consists of nine bits and all is well
again because (36 % 9) = 0.

On a final note, don't use bit fields. There are a lot of compilers
out there that don't support them (properly)

Ben Noordhuis

[1] I'm obviously not taking MMU restrictions in account here ;-)
[2] Please note that "sensible" doesn't mean "mandatory" . It would be
perfectly legal for a x86-compiler to use 32 bits for "char" instead.
Nov 14 '05 #8
What does a fella do? On the one hand, I'm told that I'm posting
improperly. On the other, I can't discern my lack of net nuchego without
observing my own posts.

We need to discuss this issue, as, in my belief, it does not arise in FAQ's.
If a single person posts under me witgh an opinion that a data type has less
than 8 bits, then you need to come to grips with the legacy of LeRoy Wentz.

MPJ

"Jack Klein" <ja*******@spam cop.net> wrote in message
news:v8******** *************** *********@4ax.c om...
On 6 Sep 2004 06:35:19 -0700, an*******@hotma il.com (Angel Lopez)
wrote in comp.lang.c:
Sorry if I am too naive, but this is my first post...
I have a binary variable (it can contain either a 0 or a 1).
Is there any way I can define a data type that uses only 1 bit. So
far I have defined it as a char variable. I've searched everywhere but
I don't seem to find any place that explains how to define this type
of data type. The closest thing I've found are bit fields in
structures, I would like something like bit fields but without the
structure.
something like
unsigned MyVariable :1;
Thanks,
Angel


This just can't be done in C. Bit-fields are only allowed inside
structures, and other than bit-fields, no object is allowed to be
smaller than sizeof(char).

If you actually have a large enough number of these values that memory
space becomes important, the FAQ for this group has an example of
packing multiple bits into larger data types.

See http://www.eskimo.com/~scs/C-faq/q20.8.html

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

Nov 14 '05 #9
Merrill & Michele wrote:

What does a fella do? On the one hand, I'm told that I'm posting
improperly. On the other, I can't discern my lack of net nuchego
without observing my own posts.


Before you go any further correct your top-posting habit. Your
reply goes after, or intermixed with, the quoted material, AFTER
snipping out anything that is not germane to your reply. That way
each article is readable and stands more or less by itself.

--
"I'm a war president. I make decisions here in the Oval Office
in foreign policy matters with war on my mind." - Bush.
"If I knew then what I know today, I would still have invaded
Iraq. It was the right decision" - G.W. Bush, 2004-08-02

Nov 14 '05 #10

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

Similar topics

3
3215
by: robert | last post by:
Hi All, I have a problem. I have written a COM DLL in VB6 to do some stuff. I need to call this DLL from a VC++ program for which I have the code. I have managed to get as far as creating the instance of the COM object and calling a method from within the C++ program - not being a C++ programmer at all, this was a huge undertaking, and I'm very proud of myself for even getting this far ;-). My Question is in relation to passing...
5
533
by: Angel Lopez | last post by:
Sorry if this appears twice but my first try didn't seem to make it. but this is my first post (hopefully)... I have a binary variable (it can contain either a 0 or a 1). Is there any way I can define a data type that uses only 1 bit. So far I have defined it as a char variable. I've searched everywhere but I don't seem to find any place that explains how to define this type of data type. The closest thing I've found are bit fields in...
9
2222
by: jc | last post by:
Hi all, I have a data type to use that I can't modify its codes. E.g. This template data type is data_type. When I declare a variable of this data_type, I write as following: data_type(8, 2) x; // Declare a data_type variable Someone can confirm me that arguments in template data types must be
12
13342
by: Just D | last post by:
All, It was possible before in Pascal, C++, etc. to define our custom data type or redefine the existing type, like in Turbo Pascal we could assume that shortint is int and use all references to shortint like all these variables became int. How can we define our own user data type in C# is we can? Just D.
13
2201
by: lane straatman | last post by:
I'm trying to figure out what data type is appropriate to represent a card in a game. The idea that I thought was going to work was a struct, foo, with two integer fields and two fields of char arrays: index cardno description suit ( 1, 1,Two of clubs ,'c') ( 2, 2,Three of clubs ,'c') ( 3, 3,Four of clubs ,'c') ( 4, 4,Five of clubs ,'c') ( 5, 5,Six of clubs ,'c')
76
4900
by: KimmoA | last post by:
First of all: I love C and think that it's beautiful. However, there is at least one MAJOR flaw: the lack of a boolean type. OK. Some of you might refer to C99 and its _Bool (what's up with the uppercase 'B' anyway?) and the header you can include (apparently) to get a real "bool". This isn't my point, however -- it should have been there from the beginning. char is a small int. We all know that. However, "char some_bool = 0;" simply...
3
5090
by: jacob navia | last post by:
Abstract: Continuing the discussion about abstract data types, in this discussion group, a string collection data type is presented, patterned after the collection in C# and similar languages (Java). It stores character strings, and resizes itself to accommodate new strings when needed. Interface: ----------
2
1551
by: =?Utf-8?B?R3JlZw==?= | last post by:
I'm using VB.Net 2005 and am defining parameters for a stored procedure using the following code snippet. MyBase.sqlAddParameter("@strExtension", _ SqlDbType.NVarChar, 4, _ objEmployee.Tables("tblEmployees").Rows(0).Item("Extension")) In this case I know the length of my variable as 4 charactes long. But, I also have SQL Variable Types of Image and NText. NText has no defined size, nore does Image as far as I can tell. What values am...
8
3268
by: mlwerth | last post by:
Dear Access Group: This is the most basic and most embarrassing of questions, but I cannot find where to change the data type of a text field that I have in Access 2003 to a number field. I've searched high and low through help databases and on the internet. The directions say to : Open the table in Design view Click the Data Type column of the field you want to change, click the
0
9161
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7732
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6522
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.