fiz
July 13, 2012, 8:04am
#1
Hello,
I never did this in ObjC and wish to know if it is possible, and how:
I have a 64-bit NSInteger. I want to store two values in it, using the “low” and the “hi” 32-bit words.
I couldn’t find the answer (but maybe I looked at the wrong place)
Thanks!
Hi,
first of all, consider that NSInteger is 32-bit in 32-bit environment.
Better use a type like int32_t
int64_t value = 17179877376;
int32_t high = value >> 32; // 4
int32_t low = value; // 8192
fiz
July 13, 2012, 10:28am
#3
Stefan,
Is that true if the project is targeting a 64bits environment?
If the project is compiled only for 64-bit then NSInteger is 64-bit
to combine two 32-bit values (low high) to one 64-bit use this
int32_t high = 4;
int32_t low = 8192;
int64_t high64 = (int64_t)high;
int64_t newValue = (high64 <<= 32) + (int64_t)low; // 17179877376
fiz
July 13, 2012, 11:06am
#5
Are int32_t, int16_t, int8_t types machine independent?
Note: my integer has to store [0…15][0…15] in its two words, they could be bytes. But the original number has to be coerced first (it will never exceed 15 in hi or low “parts”)
Yes, Apple recommends to use these types for a fixed width
PS: [0…15][0…15] is actually a 32-bit value with two 16-bit parts
not if you use unsigned char/__uint8_t then a single byte will do the trick
Right, but I assumed a signed value because of the mentioned NSInteger in the first post