Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Using Bit fields

Reply
Thread Tools

Using Bit fields

 
 
mt
Guest
Posts: n/a
 
      04-05-2011
Can I use bit fields to create my own variable of 32 bits, 20 of which
I will use for something, and use the rest as 3 1 byte values, and can
i them make an array of it?
Thanks
 
Reply With Quote
 
 
 
 
Geoff
Guest
Posts: n/a
 
      04-05-2011
On Mon, 4 Apr 2011 18:46:06 -0700 (PDT), mt <(E-Mail Removed)> wrote:

>Can I use bit fields to create my own variable of 32 bits, 20 of which
>I will use for something, and use the rest as 3 1 byte values, and can
>i them make an array of it?
>Thanks


Well, if by "byte" you mean 8 bits, then I think the answer is no.
Reason: 3 x 8 = 24, 24 + 20 = 44, 44 bits don't fit into a variable of
32 bits.

You're welcome.
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      04-05-2011
On 04/04/2011 09:46 PM, mt wrote:
> Can I use bit fields to create my own variable of 32 bits, 20 of which
> I will use for something, and use the rest as 3 1 byte values, and can
> i them make an array of it?


I'm trying to guess what you wanted, and I can't make the bit counts
come out right. Is this roughly what you're looking for?

struct my_own {
int something:20;
int one:8;
int two:8;
int three:8;
} my_array[200];

Note that (sizeof array[0])*CHAR_BITS >= 44. Is this otherwise as you
wanted?

--
James Kuyper
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      04-05-2011
On 4/4/2011 9:46 PM, mt wrote:
> Can I use bit fields to create my own variable of 32 bits, 20 of which
> I will use for something, and use the rest as 3 1 byte values, and can
> i them make an array of it?


Your bit-counting seems off, but you can make a struct of
four bit-fields, like

struct thing {
int something : 20;
int small1 : 4;
int small2 : 4;
int small3 : 4;
};

.... and you can make an array of such structs

struct thing array[42];

.... allowing `array[3].something = 9;' and `array[17].small2 = 3;'
and so on. You cannot make arrays of the individual fields, as in

struct badthing {
int something : 20;
int small[3] : 4; /* BZZZZT! */
};

There are a few portability gotchas. First, C only defines bit-
fields that are based on `int', `signed int', `unsigned int', and
(in the C99 Standard) `_Bool', and a bit-field can only have as many
bits as its base type has. (A given compiler may allow other base
types, but is not obliged to.) Since the various flavors of `int'
can be as narrow as 16 bits, the `:20' width may be too wide for some
compilers and might not work. You'll get a compile-time diagnostic
if it doesn't, though.

Second, a compiler is free to treat a plain `int' bit field as
either signed or unsigned, at its discretion. A free-standing `int'
variable is always signed, but this needn't hold for bit-fields.
This means that `array[12].small1 = -1;' may store a minus one on
some implementations, but a plus fifteen on others. The bad news is
that you might not get any compile-time diagnostic, and be stuck with
mysterious malfunctions at run-time. The good news is that if you
explicitly write `signed int small1 : 4' or `unsigned int small2 : 4'
you can override the compiler's preference. Recommendation: Always
use `signed int' or `unsigned int' as the base type for a bit-field,
never plain `int'.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
mt
Guest
Posts: n/a
 
      04-05-2011
On Apr 4, 10:21*pm, Eric Sosman <(E-Mail Removed)> wrote:
> On 4/4/2011 9:46 PM, mt wrote:
>
> > Can I use bit fields to create my own variable of 32 bits, 20 of which
> > I will use for something, and use the rest as 3 1 byte values, and can
> > i them make an array of it?

>
> * * *Your bit-counting seems off, but you can make a struct of
> four bit-fields, like
>
> * * * * struct thing {
> * * * * * * int something : 20;
> * * * * * * int small1 : 4;
> * * * * * * int small2 : 4;
> * * * * * * int small3 : 4;
> * * * * };
>
> ... and you can make an array of such structs
>
> * * * * struct thing array[42];
>
> ... allowing `array[3].something = 9;' and `array[17].small2 = 3;'
> and so on. *You cannot make arrays of the individual fields, as in
>
> * * * * struct badthing {
> * * * * * * int something : 20;
> * * * * * * int small[3] : 4; * */* BZZZZT! */
> * * * * };
>
> * * *There are a few portability gotchas. *First, C only defines bit-
> fields that are based on `int', `signed int', `unsigned int', and
> (in the C99 Standard) `_Bool', and a bit-field can only have as many
> bits as its base type has. *(A given compiler may allow other base
> types, but is not obliged to.) *Since the various flavors of `int'
> can be as narrow as 16 bits, the `:20' width may be too wide for some
> compilers and might not work. *You'll get a compile-time diagnostic
> if it doesn't, though.
>
> * * *Second, a compiler is free to treat a plain `int' bit field as
> either signed or unsigned, at its discretion. *A free-standing `int'
> variable is always signed, but this needn't hold for bit-fields.
> This means that `array[12].small1 = -1;' may store a minus one on
> some implementations, but a plus fifteen on others. *The bad news is
> that you might not get any compile-time diagnostic, and be stuck with
> mysterious malfunctions at run-time. *The good news is that if you
> explicitly write `signed int small1 : 4' or `unsigned int small2 : 4'
> you can override the compiler's preference. *Recommendation: Always
> use `signed int' or `unsigned int' as the base type for a bit-field,
> never plain `int'.
>
> --
> Eric Sosman
> (E-Mail Removed)


yes, my bit count was off because I meant to day 3 4 bit values.
Thanks for the invaluable advice. I will print it and post it on my
work board.
 
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
Bit Order in Bit Fields Mike -- Email Ignored C++ 0 05-02-2009 06:13 PM
using 32-bit flash java with 64 bit IE on a 64 bit system BP Windows 64bit 2 12-19-2008 10:59 PM
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
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