Does anyone know how to declare an integer that is exactly 25 bits long?
Does anyone know how to declare an integer that is exactly 25 bits long?
Use a bit field
something like :-
Code:struct int25 { int data : 25; };
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Just a question, though. Why do you need a 25 bit long integer?
Just use a 32 bit thing and it'd be fine, I don't think the 7 bit difference is that great. Plus, won't the compiler do some padding of its own to the struct effectively making its size 32 bytes (to align it on word boundaries)?
Thanks
Muhammad Haggag
>Plus, won't the compiler do some padding of its own to the struct effectively making its size 32 bytes (to align it on word boundaries)?
yes, it will but the int will still remain 25 bits in size reguardless of the padding...
Thanks for the help.
I need a 25 bit int as I am playing around with the actual bits inside the integer to create a random number function, and I need to ignore anything past the 25th bit.
Its an assignment, I'm learning C... don't ask me to explain the lecturers logic
Stoned Coder: Would it be possible to explain how this works (ie, what the colon does) ? Might come in handy later and prevent future posts...
Cheers
The colon tells the compiler that we are making a bit field and not an ordinary structure....
if you want you could split it up a little.....
something like...
struct int25
{
int msb : 1 // most significant bit
int top8 : 8 // next 8 bits
int mid8 : 8 // middle 8 bits
int low8 : 8 // low 8 bits
};
this would still give you a 25bit integer.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi