|
-
August 31st, 2009, 02:01 PM
#1
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
-
August 31st, 2009, 04:01 PM
#2
Re: int96 type
 Originally Posted by moralesz
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.
-
August 31st, 2009, 04:03 PM
#3
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.
-
August 31st, 2009, 06:24 PM
#4
Re: int96 type
-
August 31st, 2009, 06:36 PM
#5
Re: int96 type
 Originally Posted by moralesz
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.
-
September 1st, 2009, 02:56 PM
#6
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++.
-
September 1st, 2009, 02:59 PM
#7
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.
-
September 1st, 2009, 03:35 PM
#8
-
September 2nd, 2009, 08:22 AM
#9
Re: int96 type
Huh, it is... then what is the difference between long and int? sizeof() tells me that they're both 32 bit.
-
September 2nd, 2009, 08:28 AM
#10
Re: int96 type
 Originally Posted by ninja9578
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.
-
September 2nd, 2009, 09:17 AM
#11
Re: int96 type
 Originally Posted by monarch_dodra
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
-
September 3rd, 2009, 08:38 AM
#12
Re: int96 type
 Originally Posted by JohnW@Wessex
Actually, the standard says that
sizeof(long long) >= sizeof(long) >= sizeof(int) >= sizeof(short) >= sizeof(char)
True, I agree 100% completly
 Originally Posted by JohnW@Wessex
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...
-
September 3rd, 2009, 09:02 AM
#13
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
-
September 3rd, 2009, 09:45 AM
#14
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|