1

I'm programming a 4x4 board game in C++ using bitboards in a 64-bit machine. I only need 16 bits to implement the board. Should I use:

  • uint16_t - to reduce used space?
  • uint64_t - if operations are(?) faster with 64-bit integers, should I use them and mask the value with 0xFFFF (bitwise AND) when necessary?
  • uint_fast16_t - I just discovered this integer type, but I'm not sure how it works and if I would need a mask too?

I don't know if it helps, but my processor is: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz 1.99 GHz

André Dias
  • 103
  • 1
  • 6

1 Answers1

2
  • If you are looking to save space, use uint16_t.
  • If you are looking to save time, use uint_fast16_t.
  • uint64_t may be useful, too, for making an array of values aligned at 8-byte boundaries. This is unlikely to give you much benefits, though, because it comes at the price of wasting 75% of memory allocated for the array, along with the associated loss in cache performance.

Note: you may end up using the same type as uint64_t if your library maps uint_fast16_t to uint64_t.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • To be safe with **uint_fast16_t**, I should mask the number when I do left shifts right? Edit: in case I do right shifts later. – André Dias Sep 11 '21 at 02:09
  • 1
    @Hydrametr0nice This is correct. The system will not automatically zero out the most-significant bits outside of the 16 bits that you want to keep, so if you shift the value right at some later time, these bits would be shifted back into your value. You may want to postpone masking off the high bits until the time that you do the right shifting. – Sergey Kalinichenko Sep 11 '21 at 09:51