-5
\$\begingroup\$

There's a cool magic trick that works using the power of binary. The effect of the trick is as follows:

  1. An audience member chooses some natural number in the range of 1 to x where x is chosen by the magician.

  2. The magician hands the audience member some special cards. Each card contains some numbers from 1 to x.

  3. The audience member selects the cards which contain their number.

  4. Almost instantly, the magician can determine the original number selected.


Specification:

The numbers used for the cards are determined based on binary place value. Each card is first labeled with a power of 2. The first card becomes 1, the second becomes 2, the third becomes 4, and so on.

From now on, I will refer to card n as the card labeled with n.

To determine whether a number k is on card n, determine whether k in binary has at 1 at place value n. Consider the numbers k=13 and n=4.

K in binary is 1101. The second digit (n=4) is 1, so k=13, n=4 is a valid combination.

Goal:

Given two natural numbers 0 < n < 128 and 0 < k < 128, determine whether n appears on card k. Any reasonable input and output is allowed. Standard loopholes are banned.

This is code-golf, so the fewest bytes wins.

Test cases

\$\endgroup\$

closed as unclear what you're asking by Shaggy, Mr. Xcoder, Julian Lachniet, JungHwan Min, Peter Taylor Aug 15 '17 at 15:15

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    \$\begingroup\$ Better add the test cases directly here. \$\endgroup\$ – Mr. Xcoder Aug 15 '17 at 14:19
  • 2
    \$\begingroup\$ Also, what is determine whether k in binary has at 1 at place value n supposed to mean? Did you mean that the character in the binary representation of k at index n is 1? \$\endgroup\$ – Mr. Xcoder Aug 15 '17 at 14:22
  • \$\begingroup\$ @Mr.Xcoder Probably "at" should be "a" but not sure. \$\endgroup\$ – Erik the Outgolfer Aug 15 '17 at 14:23
  • 1
    \$\begingroup\$ Also, what did you mean by The second digit (n=4). Did you refer to the fourth digit? I am not sure I understand. \$\endgroup\$ – Mr. Xcoder Aug 15 '17 at 14:25
  • 1
    \$\begingroup\$ Does the truthy/falsy value have to be consistent? \$\endgroup\$ – Erik the Outgolfer Aug 15 '17 at 14:34
4
\$\begingroup\$

Pyth, 5 bytes

._.&E

Try it here.

Notice the cute face ._.


How?

._.&EQ  -  Q means input and is implicit.

  .&    - Bitwise AND between:
    E     - The second input and
     Q    - The first input.
._      - Sign. 0 if it equals 0, 1 otherwise.

If inconsistent values are allowed:

Pyth, 3 bytes

No cute face this time ._.

.&E

Try it here.

\$\endgroup\$
  • 2
    \$\begingroup\$ +2 for cute face, -1 for removing it \$\endgroup\$ – IsThisJavascript Aug 15 '17 at 15:25
1
\$\begingroup\$

Jelly, 1 byte

&

Try it online!

Bitwise AND Builtin

\$\endgroup\$
  • \$\begingroup\$ After I have seen your comments, I am not sure enitrely you understood. binary place value is 0 for 1, 1 for 2, 2 for 4, 3 for 8 (2^binary_place_value = n). Is that what you do in your answer? \$\endgroup\$ – Mr. Xcoder Aug 15 '17 at 14:30
  • \$\begingroup\$ @Mr.Xcoder Now I am not entirely side I understand your comment :P I'm fairly sure this is correct since it checks if the 4s place is truthy, not if the 4th digit from the right is truthy. \$\endgroup\$ – HyperNeutrino Aug 15 '17 at 14:31
  • \$\begingroup\$ @Mr.Xcoder The 4s place is the place where it adds 4 if it's 1, so 2^2. 2^n = binary_place_value \$\endgroup\$ – Erik the Outgolfer Aug 15 '17 at 14:32
  • \$\begingroup\$ If this is the intended input, then certainly simply & will do since the range is [1,x] and "Any reasonable input and output is allowed." implies truthy/falsey is fine. \$\endgroup\$ – Jonathan Allan Aug 15 '17 at 14:46
  • \$\begingroup\$ @JonathanAllan Cool, thanks. \$\endgroup\$ – HyperNeutrino Aug 15 '17 at 14:47
0
\$\begingroup\$

05AB1E, 2 bytes

Try it online!

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

Proton, 3 bytes

(&)

Try it online!

(Waiting for pull from TIO before it works on TIO but you can verify it by cloning the repository)

Functional operator of the Bitwise AND operator

\$\endgroup\$

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