uint64_t or int64_t for bitwise operations

uint64_t or int64_t for bitwise operations
typescript
Ethan Jackson

I'm writing some code which uses an array initialized with bit shifts by the compiler. I want the compiler to do the work instead of initializing the array with a for loop. I am also using the LL suffix here so that it will be 64 bit cross platform.

Is there any problem with using int64_t instead of uint64_t here?

Is there a more idiomatic or correct way to write code like this?

const int64_t map[] = { ['a'] = 1LL<<1, ['b'] = 1LL<<2, ['c'] = 1LL<<3, ['d'] = 1LL<<4, ['e'] = 1LL<<5, ['f'] = 1LL<<6, ['g'] = 1LL<<7, ['h'] = 1LL<<8, ['i'] = 1LL<<9, ['j'] = 1LL<<10, ['k'] = 1LL<<11, ['l'] = 1LL<<12, ['m'] = 1LL<<13, ['n'] = 1LL<<14, ['o'] = 1LL<<15, ['p'] = 1LL<<16, ['q'] = 1LL<<17, ['r'] = 1LL<<18, ['s'] = 1LL<<19, ['t'] = 1LL<<20, ['u'] = 1LL<<21, ['v'] = 1LL<<22, ['w'] = 1LL<<23, ['x'] = 1LL<<24, ['y'] = 1LL<<25, ['z'] = 1LL<<26, ['A'] = 1LL<<27, ['B'] = 1LL<<28, ['C'] = 1LL<<29, ['D'] = 1LL<<30, ['E'] = 1LL<<31, ['F'] = 1LL<<32, ['G'] = 1LL<<33, ['H'] = 1LL<<34, ['I'] = 1LL<<35, ['J'] = 1LL<<36, ['K'] = 1LL<<37, ['L'] = 1LL<<38, ['M'] = 1LL<<39, ['N'] = 1LL<<40, ['O'] = 1LL<<41, ['P'] = 1LL<<42, ['Q'] = 1LL<<43, ['R'] = 1LL<<44, ['S'] = 1LL<<45, ['T'] = 1LL<<46, ['U'] = 1LL<<47, ['V'] = 1LL<<48, ['W'] = 1LL<<49, ['X'] = 1LL<<50, ['Y'] = 1LL<<51, ['Z'] = 1LL<<52, };

Answer

Is there any problem with using int64_t instead of uint64_t here?

No.
All elements of the array are zero or more and <= INT64_MAX and so fit in both int64_t and uint64_t.

Should code have a shift of 63, use uint64_t and 1ULL<<63.

Related Articles