Determine length of bitstring

-4

Optimize the following function for number of tokens:

int length(int x)
{
    switch (x)
    {
    case 1: return 1;
    case 3: return 2;
    case 7: return 3;
    case 15: return 4;
    case 31: return 5;
    case 63: return 6;
    case 127: return 7;
    case 255: return 8;
    case 511: return 9;
    case 1023: return 10;
    case 2047: return 11;
    default: return 0;
    }
}

No additional cases besides the 11 shown above need to be handled. The default case will never occur (but Java needs one, or else it does not compile). Any programming language is allowed. Whole programs are also allowed.

If you embed another language inside strings, the tokens in the embedded language count as well.

The baseline is 53 tokens, since separators don't seem to count in . Have fun!

fredoverflow

Posted 2015-05-01T11:57:46.943

Reputation: 2 671

Should the submissions only be functions or full programs are also allowed ? – Optimizer – 2015-05-01T12:21:33.627

@Optimizer Whole programs are fine. – fredoverflow – 2015-05-01T12:24:39.243

2

possible duplicate of Count the number of ones in unsigned 16 bit integer

– Digital Trauma – 2015-05-01T21:37:12.633

Are you saying the code only needs to work for numbers that are one less than a power of 2? – xnor – 2015-05-01T23:21:21.970

@xnor It only needs to work for the numbers 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023 and 2047. – fredoverflow – 2015-05-04T06:55:55.270

Answers

2

Pyth, 4 tokens

slhQ

Exact same algorithm as my CJam solution.

Tokens:

s            int(
 l               log2(
  h                   1 +
   Q                  eval(raw_input()) ))

Try it online here

Optimizer

Posted 2015-05-01T11:57:46.943

Reputation: 25 836

2

gcc, 4 tokens

# define length __builtin_popcount

fredoverflow

Posted 2015-05-01T11:57:46.943

Reputation: 2 671

2

J, 3 tokens

#@:#:
  • #:: binary array (5 -> 1 0 1)
  • @:: Atop: (f@:g x -> f(g(x)))
  • #: Lenght (# 1 0 1 -> 3)

ɐɔıʇǝɥʇuʎs

Posted 2015-05-01T11:57:46.943

Reputation: 4 449

1

CJam, 5 tokens

This is a simple equation, where y = log2(x + 1)

ri)2mL

Tokens:

r           e# Read input token as string
 i          e# Convert to integer
  )         e# Increment it by 1
   2        e# This serves as the base of the log
    mL      e# Log the first number in the base of the second number. i.e. ri) log 2

Try it here

Optimizer

Posted 2015-05-01T11:57:46.943

Reputation: 25 836

1

Java, 5 tokens

x -> +" \1 \2   \3       \4               \5                               \6                                                               \7                                                                                                                               \10                                                                                                                                                                                                                                                               \11                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               \12                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               \13".charAt(x)
  1. x
  2. +
  3. string
  4. charAt
  5. x

fredoverflow

Posted 2015-05-01T11:57:46.943

Reputation: 2 671

0

Python 2, 7 tokens

This function passes the tests, but is not a reliable substitute for log2.

lambda x: len(bin(x))-2

Logic Knight

Posted 2015-05-01T11:57:46.943

Reputation: 6 622

0

Clip 10, 4 tokens

lb2n

Explanation

   n  .- the numeric value of the input -.
 b2   .- convert to binary              -.
l     .- length; i.e., number of digits -.

Ypnypn

Posted 2015-05-01T11:57:46.943

Reputation: 10 485