Problem of the Day
A new programming or logic puzzle every Mon-Fri

Bit Counter

For the numbers 1-100 print out "true" if the 2nd most least significant bit is set to 1 for that number. If not print out "false".

Permalink: http://problemotd.com/problem/bit-counter/

Comments:

  • Anonymous - 10 years, 4 months ago

    if (n Mod 4 >= 2) true

    else false

    reply permalink

  • Cha0z97 - 10 years, 4 months ago

    #include <stdio.h>

    int main() { int i; for (i=1;i<=100;i++) { i&2? printf("true\n") : printf("false\n"); } }

    reply permalink

  • Johnathan - 10 years, 4 months ago

    for i in range(1, 100):
        if format(i, '#010b')[8:-1] == '1':
            print 'true'
        else:
            print 'false'
    raw_input('')
    

    reply permalink

  • Johnathan - 10 years, 4 months ago

    I guess format(i, '08b')[6:-1] would have made more sense. I really need to read over my programs more before I submit them. Oh well.

    reply permalink

  • Nick Krichevsky - 10 years, 4 months ago

    for i in range(1,101):
        if bin(i)[-1]==1:
            print 'true'
        else:
            print 'false'
    

    reply permalink

  • (Bernardo Botelho)[http://github.com/bzxbot] - 10 years, 4 months ago

    Ruby:

    (1..100).each {|n| puts n[1] == 1}

    reply permalink

  • bzxbot - 10 years, 4 months ago

    Ruby:

    (1..100).each {|n| puts n[1] == 1}

    reply permalink

  • Anonymous - 10 years, 3 months ago

    Don’t overcomplicate things, just do a bitwise and

    for i in range(1, 101):
        print("true" if i & 2 else "false”)
    

    reply permalink

Content curated by @MaxBurstein