According to wiki shifts can be used to calculate powers of 2:
A left arithmetic shift by n is
equivalent to multiplying by 2^n
(provided the value does not
overflow), while a right arithmetic
shift by n of a two's complement value
is equivalent to dividing by 2^n and
rounding toward negative infinity.
I was always wondering if any other bitwise operators (~
,|
,&
,^
) make any mathematical sense when applied to base-10? I understand how they work, but do results of such operations can be used to calculate anything useful in decimal world?
Best Answer
In that case, yes, they can be extended to base-10 in several ways, though they aren't nearly as useful as in binary.
One idea is that
&
,|
, etc. are the same as doing arithmetic mod-2 to the individual binary digits. Ifa
andb
are single binary-digits, thenThe equivalents in base-10 would be (note again these are applied per-digit, not to the whole number)
The first three are useful when designing circuits which use BCD (
~a
being the 9's complement), such as non-graphing calculators, though we just use*
and+
rather than&
and^
when writing the equations. The first is also apparently used in some old ciphers.