-2
\$\begingroup\$

try to write shortest possible variant of logic which converts binary input by this table:

00 -> 00 01 -> 10 10 -> 01 11 -> 11 

You can use only bitwise and bitshift operations. I calculate only operations and don't care brackets, spaces, constants and etc. But you can't use any control (if, while, ternary) and arithmetic operations.

If you are using bitwise and bitshift operations, your operations number will be multipled by two:

op_num = (your_op_num << types_of_operation_num) >> 1 

edit: sorry, this is not real code-golf, because I calculate operations in logic, not characters.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ In x86 assembly, I think rol would do this in one instruction (if it could operate on only 2 bits). \$\endgroup\$ Commented Sep 23, 2013 at 9:34
  • \$\begingroup\$ Ok, so how do we count score exactly? One or two examples, maybe? \$\endgroup\$ Commented Sep 23, 2013 at 9:34
  • \$\begingroup\$ The problem is: With only bitwise operators, you can't combine numbers. And the bitwise operators can only affect bits at the same position. (1x & 1y - x and y can't affect the result of the first bit.). So every logic based solution has to use both of them. \$\endgroup\$ Commented Sep 23, 2013 at 9:34
  • \$\begingroup\$ I offer a boonty of 50 rep for the first solution that uses only bitwise operators or bitshift operators that operate on inifite range. \$\endgroup\$ Commented Sep 23, 2013 at 9:45
  • 1
    \$\begingroup\$ It's pretty trivial to show that this cannot be done with bitwise operators alone. You have to use at least one bitshift operator (since arithmetic and other operators are forbidden). \$\endgroup\$ Commented Sep 23, 2013 at 17:38

5 Answers 5

4
\$\begingroup\$

6 operations

216>>i>>i&3 

Same idea:

216>>(i<<1)&3 
\$\endgroup\$
4
\$\begingroup\$

8 operations (4 * 2)

Ok, here a real answer:

(i&1)<<1|i>>1 
\$\endgroup\$
5
  • \$\begingroup\$ I think meant is, use as less different and total operators as possible. \$\endgroup\$ Commented Sep 23, 2013 at 9:15
  • \$\begingroup\$ Because using and bitshift operations is much more easiest. \$\endgroup\$ Commented Sep 23, 2013 at 9:15
  • \$\begingroup\$ Who knows? It is tagged as code-golf so characters/bytes should count. \$\endgroup\$ Commented Sep 23, 2013 at 9:16
  • \$\begingroup\$ @JohannesKuhn Yup definetly tagged wrong. \$\endgroup\$ Commented Sep 23, 2013 at 9:17
  • \$\begingroup\$ Sorry, I don't read carefull. This is not real code-golf. \$\endgroup\$ Commented Sep 23, 2013 at 9:17
3
\$\begingroup\$

Javascript, 0 operators

alert(prompt().split('').reverse().join('')) 
\$\endgroup\$
2
  • \$\begingroup\$ This is code-golf. Characters count. \$\endgroup\$ Commented Sep 23, 2013 at 9:12
  • \$\begingroup\$ @JohannesKuhn I was confused, codegolf vs If you are using bitwise and bitshift operations, your operations number will be multipled by two: \$\endgroup\$ Commented Sep 23, 2013 at 9:13
2
\$\begingroup\$

Tcl, 0 operators

string rev $i 
\$\endgroup\$
1
\$\begingroup\$

Mathematica 0 operators?

If a string, s, is input, any of the following should work:

StringReverse[s] StringRotateLeft[s] StringRotateRight[s] 

If the input is a list of digits (the standard way to represent binary digits in Mathematica), this will work:

(# /. {{0,1} -> {1,0}, {1,0} -> {0,1}}) & 
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.