Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return type of bitwise operators in C++

As I'm implenting templated classes for small math vectors, I encounter one problem. For the arithmetic operations, the return type of T1 lhs + T2 rhs is std::common_type<T1, T2>::type. But what is the return type for the following (for example T1 signed and T2 unsigned or the contrary, or T1 char and T2 unsigned long long int etc...) :

T1 lhs & T2 rhs ?
T1 lhs | T2 rhs ?
T1 lhs ^ T2 rhs ?
T1 lhs << T2 rhs ?
T1 lhs >> T2 rhs ?

Thank you very much.

like image 969
Vincent Avatar asked Jul 31 '12 18:07

Vincent


1 Answers

I assume you are going to implement a compoment-wise bitwise operations on vectors. Essentially bitwise operations are integer operations and i see no reason why not to make their result as std::common_type<T1, T2>::type.

The result of shifts does not depend on the right operand. Just use T1 for it.

like image 157
Sergey K. Avatar answered Oct 22 '22 10:10

Sergey K.