Do two numbers contain unique powers of 2

7

1

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!

ATaco

Posted 2017-06-05T05:29:54.627

Reputation: 7 898

Can I output 0 for truthy and 1 for falsey instead? – user41805 – 2017-06-05T05:35:18.623

@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. – ATaco – 2017-06-05T05:36:26.733

Do all truthy inputs have to result in the same truthy value? – Dennis – 2017-06-05T06:02:18.627

@Dennis They can be different values, as long as they are Truthy/Falsey – ATaco – 2017-06-05T06:02:49.880

May we take inputs in binary? – Adám – 2017-06-05T07:04:04.810

If your language's default number system is binary sure, otherwise, you should use the language's default. – ATaco – 2017-06-05T07:13:51.390

5

This challenge is way too trivial.

– Octopus – 2017-06-05T15:52:06.527

Is there a language with a 1-byte NAND built-in? If so, it will win. – Magic Octopus Urn – 2017-06-09T18:36:17.067

Answers

10

Actually, 2 bytes

&Y

Try it online!

Explanation:

&Y
&   bitwise AND
 Y  boolean negation

Mego

Posted 2017-06-05T05:29:54.627

Reputation: 32 998

7

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

es1024

Posted 2017-06-05T05:29:54.627

Reputation: 8 953

1You 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. – Cody Gray – 2017-06-05T07:25:37.290

4

Jelly, 2 bytes

Try it online!

Explanation:

&¬
&   bitwise AND
 ¬  logical negation

Mego

Posted 2017-06-05T05:29:54.627

Reputation: 32 998

I was expecting this to be easy, but I didn't think of &, I was not expecting this to be two bytes easy. – ATaco – 2017-06-05T05:45:42.647

3

Ruby, 12 bytes

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

Try it online!

Sherlock9

Posted 2017-06-05T05:29:54.627

Reputation: 11 664

3

Mathematica, 12 bytes

BitAnd@##<1&

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

Greg Martin

Posted 2017-06-05T05:29:54.627

Reputation: 13 940

2

Python, 18 bytes

lambda a,b:not a&b

Try it online!

Sherlock9

Posted 2017-06-05T05:29:54.627

Reputation: 11 664

62 bytes saved using <1 rather than not. – Jonathan Allan – 2017-06-05T05:57:23.980

2

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!

Dennis

Posted 2017-06-05T05:29:54.627

Reputation: 196 637

2

05AB1E, 2 bytes

&_

Try it online!

Erik the Outgolfer

Posted 2017-06-05T05:29:54.627

Reputation: 38 134

1

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));

user41805

Posted 2017-06-05T05:29:54.627

Reputation: 16 320

1B saved: a=>b=>a&b<1 – Luke – 2017-06-05T08:03:31.270

@Luke That outputs 0 for all the testcases. In order to make that work I would have to surround a&b with parentheses. – user41805 – 2017-06-05T08:04:43.933

Oh, nvm then... – Luke – 2017-06-05T17:36:13.830

1

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).

Esolanging Fruit

Posted 2017-06-05T05:29:54.627

Reputation: 13 542

1

CJam, 4 bytes

{&!}

Try it online!

Erik the Outgolfer

Posted 2017-06-05T05:29:54.627

Reputation: 38 134

https://codegolf.stackexchange.com/a/124643/61384 – Esolanging Fruit – 2017-06-05T07:13:24.053

@Challenger5 Damn you grace period! – Erik the Outgolfer – 2017-06-05T07:13:52.110

1

R, 28 bytes

f=function(a,b)!bitwAnd(a,b)

Zahiro Mor

Posted 2017-06-05T05:29:54.627

Reputation: 371

1

R, 23 bytes

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

!bitwAnd(scan(),scan())

Zahiro Mor

Posted 2017-06-05T05:29:54.627

Reputation: 371

1

Erik the Outgolfer

Posted 2017-06-05T05:29:54.627

Reputation: 38 134

1

Pyke, 3 bytes

.&!

Try it here!

.&  -  bitwise_and(input_1, input_2)
  ! - not ^

Blue

Posted 2017-06-05T05:29:54.627

Reputation: 26 661

1

MATL, 3 bytes

Z&~

Try it at MATL Online.

Explanation

    % Implicitly grab both inputs
Z&  % Bit-wise AND
~   % Negate the result and implicitly display

Suever

Posted 2017-06-05T05:29:54.627

Reputation: 10 257

0

Pari/GP, 18 bytes

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

Try it online!

alephalpha

Posted 2017-06-05T05:29:54.627

Reputation: 23 988

0

Javascript, 28

(a,b)=>!(256&(-~a|-~b)||a&b)

C5H8NNaO4

Posted 2017-06-05T05:29:54.627

Reputation: 1 340

0

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.

steenbergh

Posted 2017-06-05T05:29:54.627

Reputation: 7 772

0

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.

Gassa

Posted 2017-06-05T05:29:54.627

Reputation: 101

0

PHP, 25 Bytes

print 1 for true and nothing for false

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

Try it online!

Jörg Hülsermann

Posted 2017-06-05T05:29:54.627

Reputation: 13 026

<?=+!($argv[1]&$argv[2]); -2 bytes – Felipe Nardi Batista – 2017-06-05T14:32:09.133

@FelipeNardiBatista Yes then the arguments must cast from string to integer.Otherwise 40&4 is false and "40"&"4" is true for example – Jörg Hülsermann – 2017-06-05T14:34:07.573

what about removing just one of the two? it worked with 40&4 – Felipe Nardi Batista – 2017-06-05T14:36:00.767

@FelipeNardiBatista right that cast both values to integer Thank You – Jörg Hülsermann – 2017-06-05T14:44:43.540

0

Lua, 19 bytes

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

Try it online!

Felipe Nardi Batista

Posted 2017-06-05T05:29:54.627

Reputation: 2 345

0

C (gcc), 18 bytes

Uses exit-code 0 as truthy.

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

Try it online!

Felipe Nardi Batista

Posted 2017-06-05T05:29:54.627

Reputation: 2 345

0

Pyth, 6 bytes

!.&hQe

Test it online!

Explanations

!.&hQe

   hQ     Get first input number
     e    Get second (implicit) input number
 .&       Bitwise and
!         Negate

Jim

Posted 2017-06-05T05:29:54.627

Reputation: 1 442

0

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.

Phlarx

Posted 2017-06-05T05:29:54.627

Reputation: 1 366

0

Java 13 bytes

a->b->(a&b)<1

Nathan Merrill

Posted 2017-06-05T05:29:54.627

Reputation: 13 591

0

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

Post Rock Garf Hunter

Posted 2017-06-05T05:29:54.627

Reputation: 55 382

0

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

abrittaf

Posted 2017-06-05T05:29:54.627

Reputation: 119