# Thread: How to set all bits in loop

1. ## 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. Don't need a loop:
Code:
`int nibbles = (1 << 4) - 1;`

3. Originally Posted by flp1969
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

4. > 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;
}```

5. Originally Posted by Salem
> 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. (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. 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```