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

## 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 binary
• 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 Sep 30 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 Sep 30 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 Sep 30 at 15:35
• The existence of DecimalBinary Two Bit Numbers™️ is a fascinating nerd-snipe problem! – Sisyphus Oct 1 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? – Jonathan Allan Oct 1 at 2:39

# Husk, 11 10 bytes

#2§¤eṁ^3ḋd


Try it online!

### Explanation

  §            Apply to input the two functions
d       Convert to list of digits
ḋ        And convert to list of binary digits
¤e          Then apply to both values
ṁ^3         Cube each and sum
#2             How many 2s are there in the list?


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 :-) – Zsolt Szilagy Oct 18 at 13:19 # Scala, 736354 52 bytes n=>Seq(n+"",n.toBinaryString)find(_ matches "10*"*2)  Try it online! Edit: • Thanks to @Tomer Shetah for pointing out that lamdbas do not require their type definition (see comments), which reduces the length from 73 to 63 bytes • Thanks to @user for adding some nice syntax tricks that further reduce the length from 63 to 54 bytes • Again thanks to @user for the comment on using find instead of exists, which saves another 2 bytes and is valid for this challenge, since it is stated that a valid solution should "output true or false (or some indicator of true or false)" • You can do it in -10 bytes. Try it online! – Tomer Shetah Oct 20 at 9:08 • I am unsure about whether it is accepted for a Scala solution to provide an anonymous expression that cannot be called without binding it to some function name and signature to make it actually callable. You exclude the val f: Int => Boolean = from your byte-counts, although it is required to call the expression. I include the binding via the (shorter) def a(n:Int)= alternative. So my question in a nutshell: is for example _.length a valid solution, or only one of val a: String => Int = _.length or def a(s: String)=s.length ? Perhaps this is a topic for a meta discussion. – cubic lettuce Oct 21 at 15:07 • It has already been discussed. Please read this post – Tomer Shetah Oct 22 at 5:16 • You can get 54 bytes. In fact, if you use find instead of exists, you can save 2 bytes more, but you'll have to change your test cases to use !=None instead. – user Nov 19 at 23:26 • -1 for being spherical lettuce and calling yourself cubic lettuce – Razetime Nov 20 at 15:36 # 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 Oct 3 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. – Kevin Cruijssen Oct 3 at 11:38 • Ah, I thought there would be a non-static toString(radix) method for some reason. – Neil Oct 3 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! # 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

• This fails for 200 – Jo King Oct 7 at 1:32
• Ah yeah I see, I'll fix that now – ZippyMagician Oct 8 at 15:28
• It should now be fixed. Thanks for pointing that out – ZippyMagician Oct 8 at 15:36
• Doesn't this fail on -1 and -3 now? – Jo King Oct 8 at 22:36
• ah... yes, you are correct. I'll fix this later – ZippyMagician Oct 9 at 2:02

# Groovy, 35 bytes

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


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.