简体   繁体   中英

How to make signed 64 bit integer using one signed integer and one unsigned integer on 32 bit processor

On 32 bit processor with 32 bit compiler I want to make a 64 bit signed integer using one of each signed and unsigned integer. Without using any predefined macro or types.

32-bit compilers will handle 64-bit numbers for you. So its unlikely you actually need this. But I'll bite. On the surface this is a pretty simple problem.

#include <stdint.h>

static inline int64_t make_int64(uint32_t low, int32_t high) {
    return (int64_t)((uint64_t)high << 32) | low;
}

static inline void split_int64(int64_t value, uint32_t *low, int32_t *high) {
    *low = value & 0xFFFFFFFF;
    *high = (int32_t)((uint32_t)(value >> 32));
}

But its always tricky/dangerous mixing signed and unsigned integers. Manually constructing an int also requires you to know how the processor formats them. We'll assume its 2s compliment little endian.

It would be helpful if you gave a full description of your requirements. For example the above example make_int64(0, -1) = -4294967296 but make_int64(1, -1) = -4294967295.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM