Thread: How to set all bits in loop

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    Many ways you can do this. I tried to make it simple so that it's easier for you to understand what's going on.
    (1 << i) basically sets the i'th bit to 1 (assuming, the rightmost bit index starts at 0). Same as calculating 2 raised to i.

    Code:
    int main ()
    {
        unsigned int Byte;
    
        for (int i = 0; i < 32; i++) {
            Byte = (1 << i);
            for (int j = 31; j >= 0; j--) {
                printf ("%d", (Byte & (1 << j) ? 1 : 0));
            }
            printf ("\n");
        }
    
        return 0;
    }
    Result:
    Code:
    00000000000000000000000000000001 - 1
    00000000000000000000000000000010 - 2
    00000000000000000000000000000100 - 4
    00000000000000000000000000001000 - 8
    00000000000000000000000000010000 - 16
    00000000000000000000000000100000 - 32
    00000000000000000000000001000000 - 64
    00000000000000000000000010000000 - 128
    00000000000000000000000100000000 - 256
    00000000000000000000001000000000 - 512
    00000000000000000000010000000000 - 1024
    00000000000000000000100000000000 - 2048
    00000000000000000001000000000000 - 4096
    00000000000000000010000000000000 - 8192
    00000000000000000100000000000000 - 16384
    00000000000000001000000000000000 - 32768
    00000000000000010000000000000000 - 65536
    00000000000000100000000000000000 - 131072
    00000000000001000000000000000000 - 262144
    00000000000010000000000000000000 - 524288
    00000000000100000000000000000000 - 1048576
    00000000001000000000000000000000 - 2097152
    00000000010000000000000000000000 - 4194304
    00000000100000000000000000000000 - 8388608
    00000001000000000000000000000000 - 16777216
    00000010000000000000000000000000 - 33554432
    00000100000000000000000000000000 - 67108864
    00001000000000000000000000000000 - 134217728
    00010000000000000000000000000000 - 268435456
    00100000000000000000000000000000 - 536870912
    01000000000000000000000000000000 - 1073741824
    10000000000000000000000000000000 - 2147483648
    This is for 32 bit integers. Manipulate the above code a bit to make it work for 4 bits. There are many-many other ways you can achieve the same result and you should try experimenting yourself, like Salem mentions, with your compiler and editor.
    If you also are familiar with C++ STL, you could try and learn the algorithm behind std::next_permutation. There's bitset's too which you could use.

    Code:
    #define all(x)  x.begin (), x.end ()
    
    int main ()
    {
        string NIBBLE = "0001";
    
        cout << NIBBLE << endl;
        next_permutation(all(NIBBLE));
        cout << NIBBLE << endl;
        next_permutation(all(NIBBLE));
        cout << NIBBLE << endl;
        next_permutation(all(NIBBLE));
        cout << NIBBLE << endl;
    
        return 0;
    }
    
    int main ()
    {
        for (int i = 0; i < 4; i++)
            cout << bitset <4> (1 << i) << endl;
    
        return 0;
    }
    Result:
    Code:
    0001
    0010
    0100
    1000
    Last edited by Zeus_; 02-29-2020 at 05:37 AM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing particular bits and detecting toggled bits
    By Nordomus in forum C++ Programming
    Replies: 11
    Last Post: 04-13-2018, 12:07 PM
  2. Bits
    By dpp in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2009, 12:28 AM
  3. Extracting certain bits from sequence of bits
    By lucaspewkas in forum C Programming
    Replies: 5
    Last Post: 10-06-2007, 12:22 AM
  4. New idea on conveting byte to bits/bits to byte
    By megablue in forum C Programming
    Replies: 10
    Last Post: 10-26-2003, 01:16 AM
  5. copy some bits into a 8 bits binary number
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 05-29-2002, 10:54 AM

Tags for this Thread