# Declaring int of specific bit size

• 08-22-2001
Natase
Declaring int of specific bit size
Does anyone know how to declare an integer that is exactly 25 bits long?
• 08-23-2001
Stoned_Coder
Use a bit field

something like :-

Code:

struct int25
{
int data : 25;
};

• 08-23-2001
Coder
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
• 08-23-2001
no-one
>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...
• 08-23-2001
Natase
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
• 08-23-2001
Stoned_Coder
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.
• 08-26-2001
Natase