The printf
format flag %x
means to print an integral value in hexadecimal.
There is a stream manipulator to accomplish this as well (std::hex
), but you're not using that. When you output an integral value to a stream with no manipulators, it outputs in base 10.
See here for more information about the printf
format flags, and here for information about stream manipulators.
The shift operator <<
works as described in the C++03 Standard (14882:2003):
5.8 Shift operators
1/ The shift operators << and >> group left-to-right.
shift-expression:
additive-expression
shift-expression << additive-expression
shift-expression >> additive-expression
The operands shall be of integral or enumeration type and integral
promotions are performed. The type of the result is that of the
promoted left operand. The behavior is undefined if the right operand
is negative, or greater than or equal to the length in bits of the
promoted left operand.
2/ The value of E1 << E2 is E1 (interpreted as a bit pattern)
left-shifted E2 bit positions; vacated bits are zero-filled. If E1 has
an unsigned type, the value of the result is E1 multiplied by the
quantity 2 raised to the power E2, reduced modulo ULONG_MAX+1 if E1
has type unsigned long, UINT_MAX+1 otherwise.
[Note: the constants ULONG_MAX and UINT_MAX are defined in the header
). ]
In your case, the value -1
in binary is all 1
s in every bit. For a 32 bit value, then:
11111111 11111111 11111111 11111111
If you shift this left 1 bit using <<
, you get:
11111111 11111111 11111111 11111110
Which in base 10 is -2. Since the operation -1<<1
uses a negative number for the LHS, the entire expression is of a signed (not unsigned) type.