# bit-fields

• 09-25-2002
kps
bit-fields
if i define a structure as :

struct
{
int a : 1 ;
int b : 10 ;
/* and blah..blah..blah*/
} myStruct;

and they say :

myStruct.a = 1;

now why wud the member 'a' get a value of -1 from this ?? i know the work around would be to use unsigned as the type....but what's the reason ?

thanx,
kps
• 09-25-2002
Cshot
That's because the most significant bit in an int represents the sign bit. So if you limit your int to 1 bit, your only possible values you can attain is 0 and -1. Use unsigned int if you want the values to be 0 and 1.

ints have the following ranges:
1-bit: -1 to 0
2-bit: -2 to 1
3-bit: -4 to 3
etc...
• 09-25-2002
Hunter2
Hmm.... just a side thought, could you make a 256-bit int using bit-fields?
• 09-25-2002
Cshot
sure, i don't see why not
• 09-25-2002
Hunter2
Wow! And if I made a 1024-bit int, that would work too? :eek:

That's a really big number!
Quote:

1.797693134862315907729305190789 e308
Could I modify it like any other int too? (i.e. crazyint.value += 3238)
• 09-25-2002
Cshot
Only if you've overloaded +=, but that's a c++ thing. It's more complicated than what you've stated though. You'll probably have to write your own functions to add,sub,mult,div and also to print.
• 09-25-2002
quzah
Quote:

Originally posted by Hunter2
Hmm.... just a side thought, could you make a 256-bit int using bit-fields?
No. Not in the true sense of the word anyway. Yeah, you could make an array of 256 bits, and use that, which is basicly what a bitfiled is. But no, you couldn't simply use standard operators on it. Furthermore, you'd have to create a whole set of functions to manipulate them.

For example, using standard numbers, there is no way you could ever do this:

myVar = myFun( 1234567890987654321 );

The only way you'd be able to do that, would be to use strings. Just using "pure numbers" as it were, this is impossible, even if this was C++ and you created your own classes.

Quzah.
• 09-25-2002
Hunter2
Fiddlesticks :(
• 09-25-2002
moi
Quote:

Originally posted by Hunter2
Fiddlesticks :(
there are big number math libraries for C available, out there somewheres on the intarweb.
• 09-25-2002
Hunter2
Well, I don't really need big numbers, so I guess I'll pass on that for now :) Maybe some other time.