32
\$\begingroup\$

Let's start by defining a Two Bit Number™:

  • It is a positive integer
  • When expressed as a binary string it has exactly 2 true bits OR
  • When expressed as a decimal number, it has exactly 2 of the numeral one, and all other numerals are zero.

Or as a sentence

A Two Bit Number™ is a number which contains exactly 2 of the numeral 1 and no other numerals besides 0, when expressed as a decimal string or a binary number.

So here area all the Two Bit Numbers™ between 0 and 256

Dec  Bin       Type
3    00000011  Binary
5    00000101  Binary
6    00000110  Binary
9    00001001  Binary
10   00001010  Binary
11   00001011  Decimal
12   00001100  Binary
17   00010001  Binary
18   00010010  Binary
20   00010100  Binary
24   00011000  Binary
33   00100001  Binary
34   00100010  Binary
36   00100100  Binary
40   00101000  Binary
48   00110000  Binary
65   01000001  Binary
66   01000010  Binary
68   01000100  Binary
72   01001000  Binary
80   01010000  Binary
96   01100000  Binary
101  01100101  Decimal
110  01101110  Decimal
129  10000001  Binary
130  10000010  Binary
132  10000100  Binary
136  10001000  Binary
144  10010000  Binary
160  10100000  Binary
192  11000000  Binary

The challenge:

  • Write some code which accepts a number and outputs true or false (or some indicator of true or false) if it is a Two Bit Number™.
  • Input will always be an integer, but it may not always be positive.
  • It can be in any language you like.
  • It's code golf, so the fewest bytes wins.
  • Please include links to an online interpreter for your code (such as tio.run).

Test Cases

Binary Two Bit Numbers™:

3
9 
18 
192
288
520
524304

Decimal Two Bit Numbers™:

11
101
1001
1010
1100
1000001
1100000000
1000000010

Non Two Bit Numbers™:

0
1
112 (any numerals over 1 prevent it being a Decimal Two Bit Number™)
200
649
-1
-3

Fun fact: I was not able to find any DecimalBinary Two Bit Numbers™ checking up to about 14 billion, and I have a hypothesis that such a number does not exist, but I have no mathematical proof. I'd be interested to hear if you can think of one.

\$\endgroup\$
20
  • 6
    \$\begingroup\$ I've sketched out a somewhat clumsy proof that no DB2B number exists. Not sure if it makes sense though \$\endgroup\$
    – Jo King
    Sep 30, 2020 at 14:07
  • 6
    \$\begingroup\$ @JoKing How do you get from 10^x+10^y = 2^x+2^b to 5^x = 2^b/10^y? It looks like you may have incorrectly treated the former expression as though it said * instead of +, and then divided both sides by 2^x*10^y. \$\endgroup\$
    – Lynn
    Sep 30, 2020 at 14:43
  • 4
    \$\begingroup\$ Input will always be an integer, but it may not always be positive. How are negative numbers encoded? With a minus in front? With a 1 in front? Using two's complement? \$\endgroup\$
    – Stef
    Sep 30, 2020 at 15:35
  • 5
    \$\begingroup\$ The existence of DecimalBinary Two Bit Numbers™️ is a fascinating nerd-snipe problem! \$\endgroup\$
    – Sisyphus
    Oct 1, 2020 at 0:38
  • 5
    \$\begingroup\$ You have -1 as not a Two Bit number (it would be in 2-s complement), you have -3 as not a Two Bit number (it would be with a minus sign character prepended). You do not have -11 as an example (again would be with a minus sign). I can only imagine that your rule is to convert to a base representation using digits in \$[-b+1..0]\$ e.g. -123 base 10 is [-1,-2,-3] \$= -1 \times 10^2 + -2 \times 10^1 + -1 \times 10^0\$. Without a spec it seems we can say all negative numbers are not Two Bit Numbers, so why, are there example ones and why do we even need to handle them? \$\endgroup\$ Oct 1, 2020 at 2:39

49 Answers 49

1
2
3
\$\begingroup\$

Chocolate, 8 bytes

ċ11€ṛ0;B

Try it online!

Link includes γ (map) for test cases.

ċ11€ṛ0;B
       B ## Binary representation (as a string) of the input
      ;  ## Make a list with that and the input
   €ṛ0   ## For each, remove all zeros
ċ11      ## Does this contain 11? (11 == "11")
\$\endgroup\$
0
3
\$\begingroup\$

PARI/GP, 46 bytes

n->(d=digits(n))*d~==2||sumdigits(n,2)==2&&n>0

Attempt This Online!

For base 2, checks whether the sum of the digits is \$2\$.

For base 10, checks whether the sum of the squares of the digits is \$2\$.

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

Pip, 12 bytes

11N[TBaa]DC0

Outputs 1 for two-bit numbers, 0 otherwise. Try It Online! Or, use as a filter to get all two-bit numbers up to 101.

Explanation

   [    ]     Construct a list containing
    TBa         the input converted to binary
       a        and the input unchanged
         DC0  Delete all occurrences of the character 0 from both
11N           Count how many times 11 occurs in the resulting list
\$\endgroup\$
3
\$\begingroup\$

Fig < 1.0.0, \$11\log_{256}(96)\approx\$ 9.054 bytes

Gu=11Oeo0wb

See the README to see how to run this

I will not let Vyxal beat me. Even though I ported it. No. Never. That thing that beat me by 0.054 bytes is not Vyxal. It is an impostor. Long live Fig!!!

Gu=11Oeo0wb # Takes 1 input
          b # Convert to binary
         w  # Wrap it in a list with the original number
      eo0   # Remove 0s
     O      # Join by nothing
  =11       # Equal to 11?
 u          # Unpack the list into the arguments of...
G           # Max
\$\endgroup\$
7
  • \$\begingroup\$ So I haven't been able to run this, but based on your explanation it looks like it always converts to binary. Can this still detect decimal two-bit numbers? Also, does it handle the negative and "two or more" cases? \$\endgroup\$
    – AJFaraday
    Jul 22, 2022 at 6:42
  • 1
    \$\begingroup\$ @AJFaraday see the w operator: "Wrap it in a list with the original number". And yes it has the same behavior as Vyxal. \$\endgroup\$
    – Seggan
    Jul 22, 2022 at 13:40
  • \$\begingroup\$ you've been beaten by 1.054 bytes >.< \$\endgroup\$
    – naffetS
    Jul 27, 2022 at 4:01
  • \$\begingroup\$ @Steffan noooooooo. time to port. \$\endgroup\$
    – Seggan
    Jul 27, 2022 at 14:13
  • 1
    \$\begingroup\$ When you realize checking if any are equal is the same as contains \$\endgroup\$
    – naffetS
    Jul 27, 2022 at 17:47
2
\$\begingroup\$

PHP, 69 67 bytes

echo($f=fn($s)=>str_replace(0,'',$s)==11)($argn)|$f(decbin($argn));

Try it online!

Pretty straightforward: treats each input as a string, replaces all zeroes by '' and tests if it (loosely) equals 11, or if the binary does

EDIT: saved 2 bytes by using 0 instead of '0'

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

APL (Dyalog Extended), 19 bytes

2∊2 10(+.×⍨⊤)¨⊢×0<⊢

Try it online!

Commented:

                0<⊢  ⍝ Is the (right) argument greater than 0
              ⊢×     ⍝ Multiply this with the argument
                     ⍝   results in 0 for negative inputs
  2 10(     )¨       ⍝ Call the next function with each of the bases 2 and 10
                     ⍝   and the non-negative number as a right argument
           ⊤         ⍝ Convert number to base digits
       +.×⍨          ⍝ The sum of squares of the digits
2∊                   ⍝ Does this contain 2?
\$\endgroup\$
2
\$\begingroup\$

C (gcc), 75 \$\cdots\$ 76 80 bytes

Added 6 bytes to fix a bug.

d;r;c;f(n){for(c=2,r=__builtin_popcount(n)!=2|n<0;n>0;n/=10)d=n%10,c-=d*d;r*=c;}

Try it online!

Returns a falsy value if \$n\$ is a Two Bit Number™️ or a truthy one otherwise.

\$\endgroup\$
4
  • \$\begingroup\$ strictly speaking __builtin_popcount is a built-in function that only exists in some compilers and not a standard function \$\endgroup\$
    – phuclv
    Oct 2, 2020 at 4:15
  • 1
    \$\begingroup\$ @phuclv true, but, "For the purposes of PPCG, a programming language is defined by its implementation." codegolf.meta.stackexchange.com/a/7833/43195 \$\endgroup\$
    – Phoenix
    Oct 2, 2020 at 4:40
  • \$\begingroup\$ @phuclv As it clearly states my answer is for GCC, __builtin_popcount is a GCC builtin. \$\endgroup\$
    – Noodle9
    Oct 2, 2020 at 10:12
  • \$\begingroup\$ 68 bytes \$\endgroup\$
    – c--
    Jul 19, 2022 at 20:59
2
\$\begingroup\$

Java 8, 67 bytes

n->(" "+n+" "+n.toString(n,2)+" ").replace("0","").contains(" 11 ")

Try it online.

Explanation:

n->                       // Method with Integer parameter and boolean return-type
  (" "                    //  Have a leading space
   +n                     //  Appended with the input
   +" "                   //  Appended with another space
   +n.toString(n,2)       //  Appended with the input as binary-String
   +" ")                  //  Appended with a trailing space
        .replace("0","")  //  Then remove all "0"s from this string
        .contains(" 11 ") //  Check if what remains contains 11 surrounded with spaces
\$\endgroup\$
3
  • \$\begingroup\$ Surely n.toString(2) suffices? \$\endgroup\$
    – Neil
    Oct 3, 2020 at 0:27
  • \$\begingroup\$ @Neil No, I'm afraid not. The first n is used as a static call. Non-golfed, it would be Integer.toString(n,2), which is a shorter variant of Integer.toBinaryString(n). What you suggest is short for Integer.toString(2), which would result in "2", since the Integer.toString(int) method will use base-10 by default. \$\endgroup\$ Oct 3, 2020 at 11:38
  • \$\begingroup\$ Ah, I thought there would be a non-static toString(radix) method for some reason. \$\endgroup\$
    – Neil
    Oct 3, 2020 at 15:51
2
\$\begingroup\$

Japt, 17 14 12 bytes

[¢Us]d_e0 ¥B

Try it

[¢Us]         - input to string base 2 , 10
     d_       - if any :
          ¥B  - are == 11 after 
       e0     - removing 0
  • Saved 3 stealing from @ovs Python answer, upvote him!
\$\endgroup\$
1
  • \$\begingroup\$ 10 bytes \$\endgroup\$
    – Shaggy
    Sep 9, 2022 at 11:48
2
\$\begingroup\$

Pyth, 22 16 bytes

-6 bytes thanks to @FryAmTheEggman

}11,v-Q\0v-.BQ\0

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ You can save some bytes by using - to remove zeros (it will automatically convert to string) and using } to test on a list rather than doing two equality comparisons. \$\endgroup\$ Oct 1, 2020 at 16:16
  • \$\begingroup\$ 14 bytes: Try it online! \$\endgroup\$ Jul 19, 2022 at 16:28
2
\$\begingroup\$

Python, 79 Bytes

lambda n:any(sum((ord(c)-48)**4 for c in f.format(n))==2for f in["{}","{0:b}"])

Explanation

I format the int as both binary and decimal. For each character, I subtract the '0' character, then raise it to the power of 4. This maps '0' to 0, '1' to 1, '2' to 16, and other digits and the '-' character to numbers greater than 16. Then, I check if the summation equals 2.

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

Octave, 67 64 bytes

@(x,p=@(b,z=dec2base(x*(x>0),b)-48)all(z<2)&sum(z)==2)p(2)|p(10)

Try it online!

If in doubt, make the code more convoluted. Somehow that tends to save bytes...

@(x,                               % Main anonymous function with 'x' as input
    p=                             % Second input 'p' with default value (no second input is given when calling function) which
      @(b,                         % consists of another anonymous function which takes base as input
          z=                       % From which it creates a second input 'z' with default value
            dec2base(       ,b)    % Which runs dec2base (convert from integer to string) using provided base
                     x             % On the input to the main anonymous function
                      *(x>0)       % Multiplied by (x>0) to return false for any negative integer passed in.
                               -48 % And converts from a string to an array of integers (one per digit)
      )
      all(z<2)&                    % Two-bit numbers must only contain 0 or 1, so need all elements in array of digits <2.
               sum(z)==2           % Sum all digits. Two-digit number if sum is 2 (two 1's)
)
p(2))||                            % Run two-bit number check in base 2
       p(10)                       % Run two-bit number check in base 10
       

First attempt,

@(x,p=@(z)all(z<50)&&sum(z-48)==2)x>0&&p(dec2bin(x))||p(num2str(x))

Try it online!

@(x,                               % Main anonymous function with 'x' as input
    p=                             % Second input 'p' with default value (no second input is given when calling function)
      @(z)                         % Default value consists of another anonymous function to check if string is two-bit
          all(z<50)&&              % Two-bit numbers must only contain '0' or '1', so need all elements in string <'2'(50).
                     sum(z-48)==2  % Convert all characters from '0'/'1' to 0/1 and sum. Two-digit if sum is 2 (two 1's)
)
x>0&&                              % Short-circuit to return false for any negative integer passed in.
     p(dec2bin(x))||               % Convert to binary string and check if two-digit, or...
                    p(num2str(x))  % Convert to decimal string and check if two-digit
\$\endgroup\$
2
\$\begingroup\$

T-SQL, 93 bytes

Returns 1 for true, 0 for false

DECLARE @y INT=@,@x INT=9WHILE @>0SELECT
@x+=@%2,@/=2PRINT
IIF(11in(@x,replace(@y,0,'')),1,0)

Try it online

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

PHP, 157 bytes

echo$argn>0&&(2==array_reduce(str_split($argn),fn($c,$i)=>$c+=$i?($i==1?1:9):0)||2==array_reduce(str_split(decbin($argn)),fn($c,$i)=>$c+=$i?($i==1?1:9):0));

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ @Kaddath beats me by almost 100 bytes :-) \$\endgroup\$ Oct 18, 2020 at 13:19
2
\$\begingroup\$

Excel, 169 bytes

=LET(n,LEN(A1),e,SUBSTITUTE(A1,1,""),d,(n-LEN(e)=2)*(LEN(SUBSTITUTE(e,0,""))=0),b,CONCAT(DEC2BIN(MOD(INT(A1/512^{0,1,2}),512))),t,(LEN(b)-LEN(SUBSTITUTE(b,1,"")))=2,d+t)

Excel (future release), 166 bytes

=LET(s,LAMBDA(x,y,SUBSTITUTE(x,y,"")),n,LEN(A1),e,s(A1,1),d,(n-LEN(e)=2)*(LEN(s(e,0))=0),b,CONCAT(DEC2BIN(MOD(INT(A1/512^{0,1,2}),512))),t,(LEN(b)-LEN(s(b,1)))=2,d+t)

This uses LAMBDA which isn't widely available yet. Not much savings.

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

Groovy, 35 34 bytes

f={1.bitCount(it)==2|it==~'10*'*2}

Try it online!

Note that this treats negative numbers as 2-bit if its binary representation has exactly two 1 bits, e.g. Integer.MIN_VALUE+1 == 10000000000000000000000000000001 would be 2-bit.

\$\endgroup\$
1
  • \$\begingroup\$ Reduced by one byte by replacing '10*10*' with '10*'*2 \$\endgroup\$
    – M. Justin
    Apr 3, 2021 at 22:12
2
\$\begingroup\$

Fig, \$9\log_{256}(96)\approx\$ 7.408 bytes

I11eo0wOb

See the README to see how to run this

Yeaa I beat Vyxal!. Also took down Chocolate as well.

I11eo0wOb # Full program
        b # Convert the number to binary
       O  # Join the list of binary
      w   # Wrap it in a list with the original number
   eo0    # Remove 0s
I11       # Contains 11?
\$\endgroup\$
1
\$\begingroup\$

Arn, 12 9 12 bytes

⁼←±Î¡Oû¨¨K²$

Try it!

Old answer:

©ou…ö6úË»˜½$

Explained

Unpacked: [+\;b--:!1#]&2

  [         Begin sequence
            ENTRY ONE
    +\        Fold with sum
        _     Variable initialized to STDIN; implied
      ;b      In binary
            ENTRY TWO
    --        Subtract one from
          _   Implied
        :!    Split on every
          1     
      #       Length
  ]         End sequence
&           Contains
  2         Literal two
\$\endgroup\$
2
  • \$\begingroup\$ Doesn't this fail on -1 and -3 now? \$\endgroup\$
    – Jo King
    Oct 8, 2020 at 22:36
  • \$\begingroup\$ ah... yes, you are correct. I'll fix this later \$\endgroup\$ Oct 9, 2020 at 2:02
1
\$\begingroup\$

K (ngn/k), 9 32 bytes

{({~#($x)^"1 0"} x)|{2=+/2\x} x}

Try it online!

Having to add more bytes because I read the challenge wrong. Doesn't work correctly with 0 and 1. Return 1 for true, and 0 for false.

Explanation:

{({~#($x)^"1 0"} x)|{2=+/2\x} x}       Main program. x is input
                    {       }          Execute a function with argument x
                         2\x           Convert to base 2
                       +/              Sum
                     2=                Equals to 2
                   |                   Or
 ({            } x)                    Execute a function with argument x
          "1 0"                        String "1 0"
         ^                             Without (set function)
     ($x)                              Input converted to string
    #                                  Length
   ~                                   Not
\$\endgroup\$
4
  • 1
    \$\begingroup\$ It should return true for 11 as well, based on the definition of Two Bit Number™️ \$\endgroup\$ Jul 19, 2022 at 16:20
  • \$\begingroup\$ @mathjunkie It's supposed to return 1 for binary and 0 for decimal, so 11 is decimal (0). I have made the summary clearer. \$\endgroup\$
    – oeuf
    Jul 19, 2022 at 23:19
  • \$\begingroup\$ Unfortunately, I think you've misunderstood the challenge. Binary and Decimal Two Bit Numbers should both return true, while Non Two Bit Numbers should return false. For example, 9, 11, 18, and 101 should all return the same value and 0, 1, and 112 should return a different value. \$\endgroup\$ Jul 19, 2022 at 23:37
  • \$\begingroup\$ @mathjunkie Yeah, I probably get the challenge wrong, will fix it soon. \$\endgroup\$
    – oeuf
    Jul 20, 2022 at 1:15
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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