27
3
Calculate n
modulo 12
for an unsigned 32 bit integer.
The Rules:
- Must work for all
n
between 0 and 23. Other numbers optional. - Must only use any of the operators
+-*
,~&^|
or<<
,>>
as commonly defined on 32 bit uints. - May use arbitrary number of constant uints.
- May not use any form of pointers, including arrays, or any
if
statements, including things that compile to if statements such as ternary operators or "greater than" operators.
The scoring:
- Operators
+ -
and the bitwise operators~ & ^ | << >>
(NOT, AND, XOR, OR, bit shifts) give a score of1
,*
gives a score of2
. - Lowest total score wins.
This question's score is currently equal to the highest number supported by many answers. – HyperNeutrino – 2017-05-03T05:49:49.107
6You might want to define the operators for users of languages other than C/Java. I understand
+-*
are add, subtract, multiply;~&^|
are bitwise NOT,AND,XOR,OR; and<< >>
are bitshifts. – Level River St – 2014-06-20T00:52:55.513@steveverrill - thanks. That is indeed the intention. – nbubis – 2014-06-20T00:53:39.283
Can I use
for i in x:y:z, .dostuff
? – Οurous – 2014-06-20T01:43:42.720Can I set a variable equal to a value to use in a expression? – xnor – 2014-06-20T02:42:46.193
@Ourous - not unless you want to count the for increments in the score. – nbubis – 2014-06-20T05:12:59.740
@xnor - you can use any uint32. – nbubis – 2014-06-20T05:13:32.387
4most compilers will optimize
n % 12
to a multiplication and a shift like in hacker's delight, so this is trivial, just output the assembly and see – phuclv – 2014-06-20T06:42:46.357Can I use
-=
? – Οurous – 2014-06-20T06:46:41.857Can I use multiple lines and the assignment operator? – Thomas Weller – 2014-06-20T11:35:11.590
@Ourous I doubt that it helps.
a -= b
can be replaced witha = a - b
which has no higher a score by the questions scoring system. – Cruncher – 2014-06-20T13:12:30.083@LưuVĩnhPhúc http://stackoverflow.com/questions/8021772/assembly-language-how-to-do-modulo Apparently, they just use a builtin.
– isaacg – 2014-06-21T08:05:18.643@isaacg they'll do it with multiply and shift like this, this, this or this instead of simple divisions like that
– phuclv – 2014-06-21T08:15:06.667Would be better to just say "may not use ternary or comparison operators", rather than assuming that they actually compile to conditional branch instructions in the underlying assembly. – None – 2014-06-22T11:07:07.450
What if my language uses
*
as the modulo operator? – Braden Best – 2014-06-23T00:10:50.730