Declaring int of specific bit size

This is a discussion on Declaring int of specific bit size within the C Programming forums, part of the General Programming Boards category; Does anyone know how to declare an integer that is exactly 25 bits long?...

  1. #1
    Registered User Natase's Avatar
    Join Date
    Aug 2001
    Posts
    123

    Question Declaring int of specific bit size

    Does anyone know how to declare an integer that is exactly 25 bits long?

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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

  3. #3
    Registered User Coder's Avatar
    Join Date
    Aug 2001
    Location
    Cairo, Egypt
    Posts
    128

    Question

    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

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    >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...

  5. #5
    Registered User Natase's Avatar
    Join Date
    Aug 2001
    Posts
    123
    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

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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

  7. #7
    Registered User Natase's Avatar
    Join Date
    Aug 2001
    Posts
    123
    Incredibly helpful... thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  2. problem with sorting
    By pinkpenguin in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 11:06 AM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 06:20 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21