CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: int96 type

  1. #1
    Join Date
    Aug 2009
    Posts
    2

    int96 type

    Hey all,

    I am trying to write a code which works with 96bit int. It means I need to read and
    store ids which are exactly 96 bits. (No more, no less)

    Since I am pretty new with C++, I don't know what type of variable I need to use.

    How can I make sure an int to use only 96 bits ? Should I write a class for that?

    Sorry if this is a very easy one but I have been googling it for 2 days now and still
    no clear idea about it.

    Thank you

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: int96 type

    Quote Originally Posted by moralesz View Post
    Should I write a class for that?
    Yes you need to make your own custom type in the form of a class.

    You could for example use 3 32-bit unsigned ints to represent a 96-bit int.

    Then you'll have to implement whatever operations you want to be able to perform on your int96 type.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: int96 type

    There is no 96-bit integer type available in the primitives. You'll have to make it a class type, presumably which internally uses 3 32-bit ints.

    Defining the operations on it correctly could be a bit of a trick. Good luck.

  4. #4
    Join Date
    Aug 2009
    Posts
    2

    Red face Re: int96 type

    Thank you guys!

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: int96 type

    Quote Originally Posted by moralesz View Post
    Thank you guys!
    If you're going to implement 96-bit arithmetics you definately should keep looking for solutions on the internet. Look for 128-bit arithmetics using 4 32-bit ints. That's more likely to have been attempted, and then you can always remove one int in the algorithms. And don't shy away from assembly here.

  6. #6
    Join Date
    Apr 2004
    Posts
    102

    Re: int96 type

    You say these are IDs?

    Will you be performing any math on these? Is there a requirement for your numbers ever to be output as base-10?

    If your answer is "no" to both, your solution is alot simpler. Just use a 3-element array of unsigned int. If you need to store things in ASCII, use hexidecimal formatting.

    If the answer is "yes" to either, use Nuzzle's solution. You are pretty much stuck to using assembly too, as you will have to use carry-in/carry-out bits which aren't directly supported in C++.

  7. #7
    Join Date
    Jan 2009
    Posts
    1,689

    Re: int96 type

    Wouldn't using a 128 bit long long be easier than 3 32-bit ints inside of the class? That way he doens't need to handle overflow from one int to the next himself, he only needs to strip overflow past the 96th bit.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: int96 type

    A long long is 64 bits.

  9. #9
    Join Date
    Jan 2009
    Posts
    1,689

    Re: int96 type

    Huh, it is... then what is the difference between long and int? sizeof() tells me that they're both 32 bit.

  10. #10
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: int96 type

    Quote Originally Posted by ninja9578 View Post
    Huh, it is... then what is the difference between long and int? sizeof() tells me that they're both 32 bit.
    It's platform dependent.

    all you know is that an int is guaranteed to be at least 32 bits, but it can be longer.

    also, sizeof(int) <= sizeof(long) (They can be equal, but an int will NEVER be bigger than a long)

    On some platforms, sizeof(int)==32 and sizeof(long)==64

    long long is non-standard c++ (but standard C99), and is guaranteed to be at least 64 bits. Maybe longer.

  11. #11
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: int96 type

    Quote Originally Posted by monarch_dodra View Post
    It's platform dependent.

    all you know is that an int is guaranteed to be at least 32 bits,
    Actually, the standard says that

    sizeof(long long) >= sizeof(long) >= sizeof(int) >= sizeof(short) >= sizeof(char)

    I once worked on system where char, short & int were all 16bit integers.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  12. #12
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: int96 type

    Quote Originally Posted by JohnW@Wessex View Post
    Actually, the standard says that

    sizeof(long long) >= sizeof(long) >= sizeof(int) >= sizeof(short) >= sizeof(char)
    True, I agree 100% completly

    Quote Originally Posted by JohnW@Wessex View Post
    I once worked on system where char, short & int were all 16bit integers.
    Yes, it was my bad saying an int is guaranteed 32 bits.
    However, the standard does guarantee a certain range for all its types.

    They are in this table
    http://home.att.net/~jackklein/c/inttypes.html#limits

    Unless that website is wrong, or you are working on a system which doesn't respect the standart...

  13. #13
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: int96 type

    It was a 16bit DSP.
    It only had 16 bit registers and there were no machine instructions for accessing them as 8bit types.

    I just double checked; char, short & int were 16bit, long was 32bit.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  14. #14
    Join Date
    Jan 2009
    Posts
    1,689

    Re: int96 type

    And if you do end up writing an int96 library, do not be afraid to post it here... might be something I would want to use

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured