7
\$\begingroup\$

Break two numbers up into their powers of 2, if they share any, return a falsey value. Otherwise, return a truthy value.

If one input is 0, the answer will always be truthy. if one input is 255, the answer will be falsey if the other is not 0.

Example

Given 5 and 3

5 = 2⁰ + 2²
3 = 2⁰ + 2¹

Both share 2⁰, return falsey.

Given 8 and 17

8  = 2³
17 = 2⁰ + 2⁴

Neither share any powers, return truthy.

I/O

Input and output can be through any standard means

Input will always be two integers in the range 0 <= n <= 255

Output should be one truthy or falsey value

Test Cases

Truthy:
1, 2
3, 4
2, 4
8, 17
248, 7

Falsey:
1, 3
6, 4
8, 8

Standard Loopholes apply.

This is , so fewest bytes wins!

\$\endgroup\$
  • \$\begingroup\$ Can I output 0 for truthy and 1 for falsey instead? \$\endgroup\$ – Kritixi Lithos Jun 5 '17 at 5:35
  • \$\begingroup\$ @KritixiLithos Although it often depends on the language as to what counts as Truthy or Falsey, this is usually acceptable. EDIT: Read the comment wrong, backwards output is not acceptable. \$\endgroup\$ – ATaco Jun 5 '17 at 5:36
  • \$\begingroup\$ Do all truthy inputs have to result in the same truthy value? \$\endgroup\$ – Dennis Jun 5 '17 at 6:02
  • \$\begingroup\$ @Dennis They can be different values, as long as they are Truthy/Falsey \$\endgroup\$ – ATaco Jun 5 '17 at 6:02
  • 5
    \$\begingroup\$ This challenge is way too trivial. \$\endgroup\$ – Octopus Jun 5 '17 at 15:52

28 Answers 28

10
\$\begingroup\$

Actually, 2 bytes

&Y

Try it online!

Explanation:

&Y
&   bitwise AND
 Y  boolean negation
\$\endgroup\$
7
\$\begingroup\$

x86 Machine Code, 5 bytes

20 C8 0F 95 C0

takes inputs in al and cl, and returns output in al, and is equivalent to:

and al, cl
setnz al
\$\endgroup\$
  • 1
    \$\begingroup\$ You could make this follow a standard calling convention (specifically, the Windows 64-bit calling convention, or Windows 32-bit __fastcall) by receiving the parameters in cl and dl. The setnz al instruction would remain the same, since a result is always returned in eax (or sub-portions thereof). That doesn't change the code size any at all, but makes the code a bit more real-world and callable from other languages. \$\endgroup\$ – Cody Gray Jun 5 '17 at 7:25
4
\$\begingroup\$

Jelly, 2 bytes

Try it online!

Explanation:

&¬
&   bitwise AND
 ¬  logical negation
\$\endgroup\$
  • \$\begingroup\$ I was expecting this to be easy, but I didn't think of &, I was not expecting this to be two bytes easy. \$\endgroup\$ – ATaco Jun 5 '17 at 5:45
3
\$\begingroup\$

Ruby, 12 bytes

->a,b{a&b<1}

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Mathematica, 12 bytes

BitAnd@##<1&

Pure function taking two positive integers as argument and returning True or False.

\$\endgroup\$
2
\$\begingroup\$

Python, 18 bytes

lambda a,b:not a&b

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Bash, 10 bytes

!(($1&$2))

Input is via command-line arguments. Output is via exit code, so 0 is truthy and 1 if falsy.

Try it online!

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 2 bytes

&_

Try it online!

\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 12 bytes

a=>b=>!(a&b)

Takes input in the currying format.

let f=

a=>b=>!(a&b)

console.log(f(1)(2));
console.log(f(3)(4));
console.log(f(2)(4));
console.log(f(8)(17));
console.log(f(248)(7));
console.log(f(1)(3));
console.log(f(6)(4));
console.log(f(8)(8));

\$\endgroup\$
  • \$\begingroup\$ 1B saved: a=>b=>a&b<1 \$\endgroup\$ – Luke Jun 5 '17 at 8:03
  • \$\begingroup\$ @Luke That outputs 0 for all the testcases. In order to make that work I would have to surround a&b with parentheses. \$\endgroup\$ – Kritixi Lithos Jun 5 '17 at 8:04
  • \$\begingroup\$ Oh, nvm then... \$\endgroup\$ – Luke Jun 5 '17 at 17:36
1
\$\begingroup\$

CJam, 23 4 bytes

q~&!

It turns out my previous solution contained an implementation of what was essentially just bitwise AND (which is silly because it used & anyway).

\$\endgroup\$
1
\$\begingroup\$

CJam, 4 bytes

{&!}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

R, 28 bytes

f=function(a,b)!bitwAnd(a,b)
\$\endgroup\$
1
\$\begingroup\$

R, 23 bytes

if allowed - this version can get multiple inputs as a1,a2,a3..... b1,b2,b3.....

!bitwAnd(scan(),scan())
\$\endgroup\$
1
\$\begingroup\$

GolfScript, 3 bytes

~&!

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Pyke, 3 bytes

.&!

Try it here!

.&  -  bitwise_and(input_1, input_2)
  ! - not ^
\$\endgroup\$
1
\$\begingroup\$

MATL, 3 bytes

Z&~

Try it at MATL Online.

Explanation

    % Implicitly grab both inputs
Z&  % Bit-wise AND
~   % Negate the result and implicitly display
\$\endgroup\$
0
\$\begingroup\$

Pari/GP, 18 bytes

x->y->!bitand(x,y)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Javascript, 28

(a,b)=>!(256&(-~a|-~b)||a&b)
\$\endgroup\$
0
\$\begingroup\$

QBIC, 12 bytes

?(: and :)=0

This returns -1 for truthy inputs, and 0otherwise. "Bitwise" is something QBasic isn't very good at, and I haven't built any of this stuff into QBIC either.

\$\endgroup\$
0
\$\begingroup\$

D, 13 bytes

(x,y)=>!(x&y)

Here, (arg1,...)=>result is the D lambda syntax, and the ! and & are common notations for boolean negation and bitwise AND.

You can find the whole program coupled with the tests at tio.run.

\$\endgroup\$
0
\$\begingroup\$

Lua, 19 bytes

a,b=...print(a&b<1)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

C (gcc), 18 bytes

Uses exit-code 0 as truthy.

f(a,b){exit(a&b);}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pyth, 6 bytes

!.&hQe

Test it online!

Explanations

!.&hQe

   hQ     Get first input number
     e    Get second (implicit) input number
 .&       Bitwise and
!         Negate
\$\endgroup\$
0
\$\begingroup\$

PHP, 25 Bytes

print 1 for true and nothing for false

<?=!($argv[1]&+$argv[2]);

Try it online!

\$\endgroup\$
  • \$\begingroup\$ <?=+!($argv[1]&$argv[2]); -2 bytes \$\endgroup\$ – Felipe Nardi Batista Jun 5 '17 at 14:32
  • \$\begingroup\$ @FelipeNardiBatista Yes then the arguments must cast from string to integer.Otherwise 40&4 is false and "40"&"4" is true for example \$\endgroup\$ – Jörg Hülsermann Jun 5 '17 at 14:34
  • \$\begingroup\$ what about removing just one of the two? it worked with 40&4 \$\endgroup\$ – Felipe Nardi Batista Jun 5 '17 at 14:36
  • \$\begingroup\$ @FelipeNardiBatista right that cast both values to integer Thank You \$\endgroup\$ – Jörg Hülsermann Jun 5 '17 at 14:44
0
\$\begingroup\$

Chip, 80 bytes

 AZ BZ CZ DZ EZ FZ GZ HZ
,[',[',[',[',[',[',[',['*Z~S
`--)--)--)--)--)--)--)~aef

Try it online!

Takes input as two consecutive bytes. (In the TIO, the characters 'A' and '$' are used, corresponding to values 65 and 36.)

This solution has eight chunks, one per bit, plus an extra chunk at the end. Looking at these chunks:

A is bit 0x01 of the input. (The letters A though H correspond to a bit each.) Z holds on to a value for the next cycle while producing the previous value. Hence, A and Z are the current and previous values of bit 0x01.

[ is an AND-gate; it simply ANDs its two inputs together. So the full row of such gates is performing a bitwise AND of the two bytes.

Finally, ) is an OR-gate. All eight AND results are OR'd together, giving us a True is there is a match, and a False otherwise. We want the inverse of that, so use a NOT-gate (~) before passing it to the output.

We output via a, e, and f. The a, which corresponds to bit 0x01 of the output, receives the result of the above computation. The other two bits e and f are unconditionally on, together forming 0x30. Therefore, the output is either 0x30 or 0x31, which are the codes for ASCII characters '0' and '1'.

The extra stuff is housekeeping, it prevents output for the first cycle (there is no previous value at this point, so there is no reason to do a comparison).


Alternate solution, also 80 bytes

A~.B~.C~.D~.E~.F~.G~.H~.
Z~<Z~<Z~<Z~<Z~<Z~<Z~<Z~<*Z~S
 a[--[--[--[--[--[--[--'ef

This is very similar in style, but uses a different algorithm. Where above we AND each pair of bits, and use OR to reduce the result to a single value, here we OR the negation of each pair of bits, then use AND to reduce.

One thing to note: we use an implicit OR here, instead of a proper gate as above. We could have used implicit OR'ing above as well, but there is no byte-savings there.

\$\endgroup\$
0
\$\begingroup\$

Java 13 bytes

a->b->(a&b)<1
\$\endgroup\$
0
\$\begingroup\$

Wise, 11 bytes

&::^~![&&]|

Due to a bug this does not currently work on tio, however it does work on the desktop version.

Here is a slightly longer version that works for both:

&::^~![?~&:]|

Try it online!

Explanation

Most of our code goes into a logical not, in fact only the first byte is required if you want to get the opposite answer.

&             #Bitwise and
 ::^~![?~&:]| #Logical not
\$\endgroup\$
0
\$\begingroup\$

Oracle SQL, 34 bytes

-- Begin test cases
WITH T (x, y, desired) AS (
  SELECT   1,  2, 1 FROM DUAL UNION ALL
  SELECT   3,  4, 1 FROM DUAL UNION ALL
  SELECT   2,  4, 1 FROM DUAL UNION ALL
  SELECT   8, 17, 1 FROM DUAL UNION ALL
  SELECT 248,  7, 1 FROM DUAL UNION ALL
  SELECT   1,  3, 0 FROM DUAL UNION ALL
  SELECT   6,  4, 0 FROM DUAL UNION ALL
  SELECT   8,  8, 0 FROM DUAL)
-- End test cases
SELECT T.*, -SIGN(BITAND(x, y)) + 1 result FROM T
/

enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.