16
2
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.
Can we implement one function that takes the desired operation as a parameter? Also, can we integer-divide by 2 as a stand-in for bit-shift? – xnor – 2015-04-03T00:44:23.723
@xnor You must provide 4 public functions that implement each of the four operators. You may also have common/helper private methods/functions that are called by all four public functions, but these will all need to be included in the golf score. – Digital Trauma – 2015-04-03T00:48:47.737
7@xnor You and you only must also implement the xnor operator ;-) – Digital Trauma – 2015-04-03T00:49:27.517
Can I produce a list of four anonymous functions? – xnor – 2015-04-03T07:14:58.040
@MariaTidalTug What's the effective difference between returning a list of four functions and manually selecting and applying one (as xnor suggested) versus having one function that accepts the selection parameter and performs the selection itself (as wolfhammer answered)? They both seem to similarly undermine the point of having four named functions, as they offload code size onto the user code. I'd even argue that the former undermines it more, as the user code is probably more complex in that case than in the latter case. – Runer112 – 2015-04-03T14:43:29.570
@Runer112 good point. – Digital Trauma – 2015-04-03T16:01:34.010
@xnor. Runer has a good point. Sorry for the rule change. I'm banning anonymous functions. – Digital Trauma – 2015-04-03T16:02:13.773
Are there any restrictions on the names of the functions? How about a dictionary of four functions, so you can invoke a function by a letter? – xnor – 2015-04-03T20:08:46.420
@xnor 4 named functions. Call them what you like as long as they are named.
a
,o
,n
andx
will do just fine :) – Digital Trauma – 2015-04-03T20:48:48.193