简体   繁体   中英

How to add 2^63 to a signed 64-bit integer and cast it to a unsigned 64-bit integer without using 128-bit integer in the middle

I am writing a function that map signed integer space to unsigned integer space. But I want the negative number map before the positive number.

The method I am thinking of work like this let say we have a signed 64-bit integer a.

  1. Cast a to 128-bit
  2. Add 2 63 to a
  3. Cast it to unsigned 64-bit

However, the problem is, the C implementation I am working with does not have 128-bit integers.

Is there any efficient way of doing this?

Arithmetic in unsigned types is modulo 2 n (that is, it wraps around).

So all you need to do is:

  1. Cast a to unsigned 64-bit
  2. Add 2 63 to a

just toggle MSB bit :
1. cast a to unsigned 64-bit
2. xor with 2 63 (or you may: add 2 63 ).

which means you need to change (xor) just one bit (bit 63) sample output:

a                    hex(a)              map(a)  
-9223372036854775808 0x8000000000000000 0x0000000000000000
-9223372036854775807 0x8000000000000001 0x0000000000000001
-9223372036854775806 0x8000000000000002 0x0000000000000002
-9223372036854775805 0x8000000000000003 0x0000000000000003
               -1000 0xfffffffffffffc18 0x7ffffffffffffc18
                 -10 0xfffffffffffffff6 0x7ffffffffffffff6
                  -1 0xffffffffffffffff 0x7fffffffffffffff
                   0 0x0000000000000000 0x8000000000000000
                   1 0x0000000000000001 0x8000000000000001
                  10 0x000000000000000a 0x800000000000000a
                 100 0x0000000000000064 0x8000000000000064
                1000 0x00000000000003e8 0x80000000000003e8
 9223372036854775805 0x7ffffffffffffffd 0xfffffffffffffffd
 9223372036854775806 0x7ffffffffffffffe 0xfffffffffffffffe
 9223372036854775807 0x7fffffffffffffff 0xffffffffffffffff  

sample code (using add // xor ):

#include <stdio.h> 
#include <stdint.h>
#include <inttypes.h>
uint64_t show(int64_t n){
    uint64_t u = (uint64_t)n;
    u += (uint64_t)1 << 63;  // u ^= (uint64_t)1 << 63;
    printf("%21"PRId64" %21"PRIu64" 0x%016"PRIx64"\n", n, u, u); 
    return u;
}
int main()
{ 
    show(-1000);
    show(-10);
    show(-1);
    show(0);
    show(1);
    show(10);
    show(100);
    show(1000); 
    return 0;
}

test sample code using union to demonstrate bit xor (just little-endian):

#include <stdio.h> 
#include <stdint.h>
#include <inttypes.h> 

typedef union {
    uint8_t b[8];
    int64_t i;
    uint64_t u;
}T64bit;

uint64_t show(int64_t n){
    T64bit t;
    t.i = n;
    t.b[7] ^= 0x80;   // t.b[7] += 0x80;
    printf("%21"PRId64" %21"PRIu64" 0x%016"PRIx64"\n", n, t.u, t.u); 
    return t.u;
}
int main()
{
    show(-1000);
    show(-10);
    show(-1);
    show(0);
    show(1);
    show(10);
    show(100);
    show(1000);
    return 0;
}

output:

        -1000   9223372036854774808 0x7ffffffffffffc18
          -10   9223372036854775798 0x7ffffffffffffff6
           -1   9223372036854775807 0x7fffffffffffffff
            0   9223372036854775808 0x8000000000000000
            1   9223372036854775809 0x8000000000000001
           10   9223372036854775818 0x800000000000000a
          100   9223372036854775908 0x8000000000000064
         1000   9223372036854776808 0x80000000000003e8

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