Pattern 0s and 1s

sir in this lecture it is said that we can switch between 0 and 1 with the help of val=1-val.
but i want to use bitwise not operator (~)in order to flip 0-1 and 1-0.
i used that in program but it shows that ~1 is -2 and ~0 is -1.

here is my code:

#include
using namespace std;
int main()
{
int n, row, col, val;
cout<<“How many rows u want to print ?”<<endl;
cin>>n;
for(row=1; row<=n; row++)
{
val = row%2==0 ? 1 : 0;
for(col=1; col<=row; col++)
{
cout<<val;
val = (~val);
}
cout<<endl;
}
return 0;
}

and the output is like this:

How many rows u want to print ?
6
0
1-2
0-10
1-21-2
0-10-10
1-21-21-2

@pushkar24sharma use ! operator !1=0 and !0=1. ~ operator negates all the 32 bits thats why the number becomes negative garbage
hope its clear if yes dont forget to hit like and mark resolved otherwise ask me :smiley:

1 Like

okk thank u @vatsal38 sir. i got it.
:smiley:

@pushkar24sharma you are welcome :smiley:

1 Like