Thread: How to set all bits in loop

  1. #1
    Registered User
    Join Date
    Feb 2020
    Posts
    23

    How to set all bits in loop

    I want to set all bits in loop

    nibble = 0000; // bits order (4321)


    nibble = nibble|nibble << 1 // set 2nd bit = 0010
    nibble = nibble|nibble << 2 // set 3rd bit = 0100
    nibble = nibble|nibble << 3 // set 4th but = 1000
    nibble = nibble| 1 // set first bit

    Code:
    #include<stdio.h>
    int main ()
    {
    	int i;
    	int nibble = 0000;
    	
    	for (i = 0; i < 4; i++)
    	{
    		
    	}
    	
    	return 0;
    }
    How to set each bit in loop?

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Don't need a loop:
    Code:
    int nibbles = (1 << 4) - 1;

  3. #3
    Registered User
    Join Date
    Feb 2020
    Posts
    23
    Quote Originally Posted by flp1969 View Post
    Don't need a loop:
    Code:
    int nibbles = (1 << 4) - 1;
    I did not understand your bit shifting (1 << 4) - 1;

    what result it (1 << 4) gives ?

    What happens if I wan to shift only one bit

    nibble = 0 <<1 = 0
    nibble = 1 <<1 = 1
    nibble = 1 <<0 = 1
    Last edited by Parth12; 02-27-2020 at 08:35 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > what result it (1 << 4) gives ?
    You've got an editor and compiler in front of you, try it.

    Code:
        for (i = 0; i < 4; i++)
        {
             nibble |= 1<<i;
        }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2020
    Posts
    23
    Quote Originally Posted by Salem View Post
    > what result it (1 << 4) gives ?
    You've got an editor and compiler in front of you, try it.
    given nibble i = 0000 (length 4 bits )
    Code:
     0001 // set first bit
    0010  //set only second bit
    0100  //set only third bit
    1000 // set only fourth bit
    How to set single bit at time as shown in table
    Code:
    #include<stdio.h>
    
    
    int main ()
    {
      int i = 0;
      int N = 0;
      
      for ( i = 0; i < 4; i++ )
      {
          printf("N = %d", N); 
      }    
    
    
    return 0;
    }

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    (1 << 4) in binary: 0b10000

    It is 00001 shifted left 4 bits.

    0b10000 - 1 = 0b1111 (all 4 bits set).

    Let's say you want only 1 bit set:

    (1 << 1) - 1 = 0b0010 - 1 = 0b0001

    Or 6 bits set:

    (1 << 6) - 1 = 0b1000000 - 1 = 0b111111

  7. #7
    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