preshing / cpp11-on-multicore

Various synchronization primitives for multithreaded applications in C++11.
zlib License
518 stars 103 forks source link

Custom bitfield types (signed fields)? #11

Closed nyanpasu64 closed 4 years ago

nyanpasu64 commented 4 years ago

If you have a 64-bit bitfield, is it possible to mark certain fields as signed, for example by passing in a custom return type of int16_t?

preshing commented 4 years ago

You could probably do this. Use std::is_signed<T>::value to determine if T is signed. You can use that in operator T() and, if it's signed, sign-extend the most significant bit. Here are a few possible ways to do that: https://stackoverflow.com/questions/5814072/sign-extend-a-nine-bit-number-in-c