Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > define a data type of 1 bit size

Reply
Thread Tools

define a data type of 1 bit size

 
 
Angel Lopez
Guest
Posts: n/a
 
      09-06-2004
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
 
Reply With Quote
 
 
 
 
Alex Fraser
Guest
Posts: n/a
 
      09-06-2004
"Angel Lopez" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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 microcontrollers (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


 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      09-06-2004
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",myvar);
that will print 1 and not 78.

jacob
 
Reply With Quote
 
Merrill & Michele
Guest
Posts: n/a
 
      09-06-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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



 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      09-06-2004
On Mon, 06 Sep 2004 16:50:46 +0200, jacob navia
<(E-Mail Removed)> 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",myvar);
> 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.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      09-06-2004
On 6 Sep 2004 06:35:19 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      09-06-2004
On Mon, 6 Sep 2004 10:32:09 -0500, "Merrill & Michele"
<(E-Mail Removed)> 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.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Ben Noordhuis
Guest
Posts: n/a
 
      09-07-2004
"Merrill & Michele" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...

> 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 % = 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.
 
Reply With Quote
 
Merrill & Michele
Guest
Posts: n/a
 
      09-07-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 6 Sep 2004 06:35:19 -0700, (E-Mail Removed) (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.learn.c-c++
> http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-07-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the point of having 16 bit colour if a computer monitor can only display 8 bit colour? How do you edit 16 bit colour when you can only see 8 bit? Scotius Digital Photography 6 07-13-2010 03:33 AM
#define ALLOCIT(Type) ((Type*) malloc (sizeof (Type))) Yevgen Muntyan C Programming 10 02-13-2007 02:52 AM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Computer Support 0 09-17-2004 08:15 PM
define a data type of 1 bit size Angel Lopez C Programming 5 09-10-2004 02:37 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM



Advertisments