Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Result of bitwise operator in C++

Tags:

c++

Testing a couple of compilers (Comeau, g++) confirms that the result of a bitwise operator of some "integer type" is an int:

void foo( unsigned char );
void foo( unsigned short );

unsigned char a, b;

foo (a | b);

I would have expected the type of "a | b" to be an unsigned char, as both operands are unsigned char, but the compilers say that the result is an int, and the call to foo() is ambiguous. Why is the language designed so that the result is an int, or is this implementation dependent?

Thanks,

like image 276
Andrew Avatar asked Jul 28 '10 21:07

Andrew


People also ask

What is the output of bitwise operator?

The bitwise OR operator produces an output of 1 if either one of the corresponding bits is 1. Otherwise, the output is zero. Example 1: The bitwise OR operation of two one-bit operands.

What does bitwise operator do in C?

The Bitwise Operator in C is a type of operator that operates on bit arrays, bit strings, and tweaking binary values with individual bits at the bit level. For handling electronics and IoT-related operations, programmers use bitwise operators. It can operate faster at a bit level.

What is the result of the bitwise operation 1?

The result of the bitwise AND operation is 1 if both the bits have the value as 1; otherwise, the result is always 0. As we can see, two variables are compared bit by bit.

What is the result of the bitwise operation 1 & 1?

1 & 1 = 1 , 0 & 1 = 0 . That's how the operator is defined. Any bit except the last one is 0 because it's 0 in 1.


2 Answers

This is in fact standard C++ behavior (ISO/IEC 14882):

5.13/1 Bitwise inclusive OR operator

The usual arithmetic conversions are performed; the result is the bitwise inclusive OR function of its operands. The operator applies only to integral or enumeration operands.

5/9 Usual arithmetic conversions

Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:

  • If either operand is of type long double, the other shall be converted to long double.
  • Otherwise, if either operand is double, the other shall be converted to double.
  • Otherwise, if either operand is float, the other shall be converted to float.
  • Otherwise, the integral promotions shall be performed on both operands.
  • ...

4.5/1 Integral Promotions

An rvalue of type char, signed char, unsigned char, short int, or unsigned short int can be converted to an rvalue of type int if int can represent all the values of the source type; otherwise, the source rvalue can be converted to an rvalue of type unsigned int.

I think it has to do with int supposedly being the "natural" size for the execution environment to allow for efficient arithmetic (see Charles Bailey's answer).

like image 74
In silico Avatar answered Oct 26 '22 13:10

In silico


I would have expected the type of "a | b" to be an unsigned char, as both operands are unsigned char,

My reading of some beginner C books in past left the impression that bitwise operators were left in the language solely for the purpose of the system programming and generally should be avoided.

The operators are performed by the CPU itself. CPU uses for operands registers (which are surely larger than char) and thus compiler cannot know how much bits of a register would be affected by the operation. To not to loose the full result of the operation, compiler upcasts the result to the proper operation. AFAICT.

Why is the language designed so that the result is an int, or is this implementation dependent?

Bit-level representation of data types is in fact implementation defined. That might be the reason why apparently bit-wise operations are also implementation defined.

Though C99 defines in 6.2.6.2 Integer types how they should appear and behave (and later how bitwise operations should work) the particular chapter gives a lot of freedom to the implementation.

like image 32
Dummy00001 Avatar answered Oct 26 '22 14:10

Dummy00001