74
\$\begingroup\$

An integer is binary-heavy if its binary representation contains more 1s than 0s while ignoring leading zeroes. For example 1 is binary-heavy, as its binary representation is simply 1, however 4 is not binary heavy, as its binary representation is 100. In the event of a tie (for example 2, with a binary representation of 10), the number is not considered binary-heavy.

Given a positive integer as input, output a truthy value if it is binary-heavy, and a falsey value if it is not.

Testcases

Format: input -> binary -> output

1          ->                                1 -> True
2          ->                               10 -> False
4          ->                              100 -> False
5          ->                              101 -> True
60         ->                           111100 -> True
316        ->                        100111100 -> True
632        ->                       1001111000 -> False
2147483647 ->  1111111111111111111111111111111 -> True
2147483648 -> 10000000000000000000000000000000 -> False

Scoring

This is so fewest bytes in each language wins

\$\endgroup\$
8
  • \$\begingroup\$ What if my language can't handle the last test case because it's outside the bounds of what's considered a positive integer? \$\endgroup\$ Jul 13, 2017 at 14:52
  • 1
    \$\begingroup\$ @musicman523 afaik Standard I/O rules state that you only have to accept numbers representable by your language's number format. Note that "gaming" this by using something like boolfuck is considered a Standard Loophole \$\endgroup\$
    – Mayube
    Jul 13, 2017 at 14:53
  • \$\begingroup\$ Does any truthy/falsy value count or do we need two distinct values? \$\endgroup\$ Jul 13, 2017 at 15:22
  • \$\begingroup\$ @EriktheOutgolfer any value \$\endgroup\$
    – Mayube
    Jul 13, 2017 at 15:35
  • 11
    \$\begingroup\$ Aka A072600, if this helps anybody. \$\endgroup\$
    – dcsohl
    Jul 13, 2017 at 17:59

97 Answers 97

1 2 3
4
0
\$\begingroup\$

CPU x86 instruction set, 18 bytes

00000750  8B4C2404          mov ecx,[esp+0x4]
00000754  31C0              xor eax,eax
00000756  D1E9              shr ecx,1
00000758  7302              jnc 0x75c
0000075A  40                inc eax
0000075B  40                inc eax
0000075C  48                dec eax
0000075D  85C9              test ecx,ecx
0000075F  75F5              jnz 0x756
00000761  C3                ret

it return >0 if the number is "binary-heavy" else if result is <=0 it is not "binary-heavy". Code for test it and see how to call it:

; nasmw -fobj  this.asm
; bcc32 -v  this.obj

section _DATA use32 public class=DATA

global _main
global _BinHev
extern _printf
fmt db "%u -> %d" , 13, 10, 0, 0

section _TEXT use32 public class=CODE

      align   8
_BinHev:  
      mov     ecx,  dword[esp+4]
      xor     eax,  eax
.0:   shr     ecx,  1
      jnc     .1
      inc     eax
      inc     eax
.1:   dec     eax
      test    ecx,  ecx
      jnz     .0
.z:   ret

_main:    
      pushad
      mov     ebx,  1
      push    ebx
      call    _BinHev
      add     esp,  4
      push    eax
      push    ebx
      push    fmt
      call    _printf
      add     esp,  12
      mov     ebx,  2
      push    ebx
      call    _BinHev
      add     esp,  4
      push    eax
      push    ebx
      push    fmt
      call    _printf
      add     esp,  12
      mov     ebx,  4
      push    ebx
      call    _BinHev
      add     esp,  4
      push    eax
      push    ebx
      push    fmt
      call    _printf
      add     esp,  12
      mov     ebx,  5
      push    ebx
      call    _BinHev
      add     esp,  4
      push    eax
      push    ebx
      push    fmt
      call    _printf
      add     esp,  12
      mov     ebx,  60
      push    ebx
      call    _BinHev
      add     esp,  4
      push    eax
      push    ebx
      push    fmt
      call    _printf
      add     esp,  12
      mov     ebx,  316
      push    ebx
      call    _BinHev
      add     esp,  4
      push    eax
      push    ebx
      push    fmt
      call    _printf
      add     esp,  12
      mov     ebx,  632
      push    ebx
      call    _BinHev
      add     esp,  4
      push    eax
      push    ebx
      push    fmt
      call    _printf
      add     esp,  12
      mov     ebx,  2147483647
      push    ebx
      call    _BinHev
      add     esp,  4
      push    eax
      push    ebx
      push    fmt
      call    _printf
      add     esp,  12
      mov     ebx,  2147483648
      push    ebx
      call    _BinHev
      add     esp,  4
      push    eax
      push    ebx
      push    fmt
      call    _printf
      add     esp,  12

      popad
      mov     eax,  0
      ret

results:

1 -> 1
2 -> 0
4 -> -1
5 -> 1
60 -> 2
316 -> 1
632 -> 0
2147483647 -> 31
2147483648 -> -30
\$\endgroup\$
0
\$\begingroup\$

bash 76

Pure bash!

i=$1;for((r=$[$1&1];i>>=1;)){ r=$[i&1]$r;};i=${#r};s=${r//1};((i>(${#s}*2)))

Demo:

isbinheavy() {
    i=$1;for((r=$[$1&1];i>>=1;)){ r=$[i&1]$r;};i=${#r};s=${r//1};((i>(${#s}*2)))
}


for val in 1 2 4 5 60 316 632 2147483647 2147483648 ;do
    if isbinheavy $val; then
        res=True
    else
        res=False
    fi
    printf "%-12s -> %32s -> %s\n" $val $r $res
done

Will render:

1            ->                                1 -> True
2            ->                               10 -> False
4            ->                              100 -> False
5            ->                              101 -> True
60           ->                           111100 -> True
316          ->                        100111100 -> True
632          ->                       1001111000 -> False
2147483647   ->  1111111111111111111111111111111 -> True
2147483648   -> 10000000000000000000000000000000 -> False

Explanation:

  1. Set variable i to submited integer
  2. Begin for loop by
    1. setting variable r to low significant bit of $1
    2. set end of loop on i variable, shifting them by 1 bit on each check
  3. In loop:
    1. add low significant bit of resulted $i on left side of $r
  4. Then store into variable i, length of $r
  5. Drop all 0 from string $r then store string into variable s.
  6. numerically test if $i > 2 x length of $s
\$\endgroup\$
0
\$\begingroup\$

C# (.NET Core), 48 bytes

bool f(int x,int c=0)=>x<1?c>0:f(x/2,c-1+x%2*2);

Try it online!

There were already a few C# solutions, but this is the first recursive one.

An optional counter argument is use to count 0's and 1's. A 0 will decrement the counter and a 1 will increment it. We are done when no more 1's are present, a positive counter indicates there were a greater number of 1's than 0's.

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

MBASIC, 86 bytes

1 INPUT N
2 Q=INT(N/2):IF N MOD 2=1 THEN O=O+1 ELSE Z=Z+1
3 N=Q:IF N>0 THEN 2
4 PRINT O>Z

Prints -1 for true, 0 for false

Examples:

? 316
-1

? 632
 0
\$\endgroup\$
0
\$\begingroup\$

Japt, 9 8 bytes

2ƤèXÃr<

Try it or run all test cases

2ƤèXÃr<     :Implicit input of integer U
2Æ           :Map each X in the range [0,2)
  ¤          :  Convert U to binary string
   èX        :  Count the occurrences of X
     Ã       :End map
      r<     :Reduce by less than
\$\endgroup\$
0
\$\begingroup\$

C# (.NET Core), 98 bytes

Way too long xD

y=>{var b=Convert.ToString(y,2);for(int i=y=0;i<b.Length;)y+=b[i++]=='1'?1:-1;return y>0?1>0:1<0;}

Try it online!

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

GNU AWK, 55 52 bytes

{for(b=0;$1;$1=rshift($1,1))$1%2?b++:b--;print(b>0)}

Step by step:

{
for(
    b=0;            # at the beginning of each loop, starts over the bit count
    $1;             # loops until input reaches zero
                    # (positive values return true evaluation)
    $1=rshift($1,1) # at the end of each loop, right shift bitwise the input by 1 bit
   )
      $1%2?b++:b--; # if input is even, +1 to the bit count; -1 if odd
print(b>0)          # after the looping, prints the evaluation of b>0; 1 if true, 0 if false
}

Try it online!

\$\endgroup\$
1 2 3
4

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.