I like to golf in dc
, but I'm sometimes frustrated because dc
doesn't have bitwise operations.
Challenge
Provide four named functions which implement the equivalent of the c bitwise operations &
, |
, ~
and ^
(bitwise AND, OR, NOT and XOR). Each function will take two operands (~
takes only one) which are at least 32-bit unsigned integers. Each function will return an unsigned integer of the same bit-width as the operands.
Restriction
You may only use operations that are supported by dc
. These are:
+
-
*
/
Arithmetic addition, subtraction, multiplication and division~
modulo (or divmod if your language supports it)^
exponentiation|
modular exponentiationv
square root>
>=
==
!=
<=
<
standard equality/inequality operators>>
<<
bit shift operators.dc
doesn't have these, but since they are trivially implemented in terms of division/multiplication by powers of 2, then I will allow these.
Control structures in dc
my be clumsily built using (recursive) macros and (in)equality operations. You may use whatever built-in control structures your language has.
You may also use logical operators &&
||
!
, even though these aren't directly available in dc
.
You must not use the bitwise operators &
, |
, ~
and ^
or any functions that trivially implement them.
In addition you must not use built-in base-conversion-of-string operators or functions.
Please also consider providing a test program or online compiler snippet (not included in the golf score) to help verify your answer.