Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Bit floating sequence

A bit floats from the LSB to the MSB moving one position each time until it floats to the top of the container:

0000
0001
0010
0100
1000

Once one bit floats to the top, another bit begins its journey and it stops when it meets other bit:

1001
1010
1100

This happens until the container is filled with bits:

1101
1110
1111

Challenge

Given an integer number, output the "Bit floating sequence" for a container of that number of bits.

  • Each term of the sequence can be separated by any separator of your choice.
  • Edit: Sequence must be shown as decimal integer numbers, starting by the first therm: 0.
  • The container size sould be greater than zero and up to the number of bits of the bigest integer suported by the language of your choice. You can assume that the input always match this requirement.

Examples

Only the numeric sequence is required, binary representation is shown as example:

  • For 1: 0 1

    0 -> 0
    1 -> 1
    
  • For 3: 0 1 2 4 5 6 7

    000 -> 0
    001 -> 1
    010 -> 2
    100 -> 4
    101 -> 5
    110 -> 6
    111 -> 7
    
  • For 4: 0 1 2 4 8 9 10 12 13 14 15

    0000 -> 0
    0001 -> 1
    0010 -> 2
    0100 -> 4
    1000 -> 8
    1001 -> 9
    1010 -> 10
    1100 -> 12
    1101 -> 13
    1110 -> 14
    1111 -> 15
    
  • For 8: 0 1 2 4 8 16 32 64 128 129 130 132 136 144 160 192 193 194 196 200 208 224 225 226 228 232 240 241 242 244 248 249 250 252 253 254 255

    00000000 -> 0
    00000001 -> 1
    00000010 -> 2
    00000100 -> 4
    00001000 -> 8
    ...
    ...
    ...
    11111000 -> 248
    11111001 -> 249
    11111010 -> 250
    11111100 -> 252
    11111101 -> 253
    11111110 -> 254
    11111111 -> 255
    

Answer*

Draft saved
Draft discarded
Cancel
2
  • 2
    \$\begingroup\$ R_2 -> Ḷ’ for -1. is the only sensible range, I really wish 05AB1E had a single-byter for it. \$\endgroup\$ Commented Sep 6, 2019 at 15:29
  • \$\begingroup\$ @Grimy Ah, how did I miss that one. I searched for range and must have skipped passed it somehow.. >.> Thanks! \$\endgroup\$ Commented Sep 6, 2019 at 16:27

AltStyle によって変換されたページ (->オリジナル) /