Is that number a Two Bit Number™?

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.

• I've sketched out a somewhat clumsy proof that no DB2B number exists. Not sure if it makes sense though
– Jo King
Commented Sep 30, 2020 at 14:07
• @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.
– lynn
Commented Sep 30, 2020 at 14:43
• 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?
– Stef
Commented Sep 30, 2020 at 15:35
• The existence of DecimalBinary Two Bit Numbers™️ is a fascinating nerd-snipe problem! Commented Oct 1, 2020 at 0:38
• 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? Commented Oct 1, 2020 at 2:39

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


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\$$.

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


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

• 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? Commented Jul 22, 2022 at 6:42
• @AJFaraday see the w operator: "Wrap it in a list with the original number". And yes it has the same behavior as Vyxal. Commented Jul 22, 2022 at 13:40
• you've been beaten by 1.054 bytes >.< Commented Jul 27, 2022 at 4:01
• @Steffan noooooooo. time to port. Commented Jul 27, 2022 at 14:13
• When you realize checking if any are equal is the same as contains Commented Jul 27, 2022 at 17:47

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'

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?


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.

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

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

• Surely n.toString(2) suffices?
– Neil
Commented Oct 3, 2020 at 0:27
• @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. Commented Oct 3, 2020 at 11:38
• Ah, I thought there would be a non-static toString(radix) method for some reason.
– Neil
Commented Oct 3, 2020 at 15:51

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!
• 10 bytes Commented Sep 9, 2022 at 11:48

Pyth, 22 16 bytes

-6 bytes thanks to @FryAmTheEggman

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


Try it online!

• 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. Commented Oct 1, 2020 at 16:16
• 14 bytes: Try it online! Commented Jul 19, 2022 at 16:28

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.

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


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

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! • @Kaddath beats me by almost 100 bytes :-) Commented Oct 18, 2020 at 13:19 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. 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. • Reduced by one byte by replacing '10*10*' with '10*'*2 Commented Apr 3, 2021 at 22:12 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?  Arn, 12 9 12 bytes ⁼←±Î¡Oû¨¨K²$


Try it!

©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  • Doesn't this fail on -1 and -3 now? – Jo King Commented Oct 8, 2020 at 22:36 • ah... yes, you are correct. I'll fix this later Commented Oct 9, 2020 at 2:02 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

• It should return true for 11 as well, based on the definition of Two Bit Number™️ Commented Jul 19, 2022 at 16:20
• @mathjunkie It's supposed to return 1 for binary and 0 for decimal, so 11 is decimal (0). I have made the summary clearer.
– oeuf
Commented Jul 19, 2022 at 23:19
• 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. Commented Jul 19, 2022 at 23:37
• @mathjunkie Yeah, I probably get the challenge wrong, will fix it soon.
– oeuf
Commented Jul 20, 2022 at 1:15