i am unable to understand the code. can anyone please explain.
printf("%d",i & 1)
wat is the &1??
int i;
for(i=0;i<10;++i)
printf("%d",i &1);
i am unable to understand the code. can anyone please explain.
printf("%d",i & 1)
wat is the &1??
int i;
for(i=0;i<10;++i)
printf("%d",i &1);
this might help you:
http://www.somacon.com/p125.php
The & is binary bitwise AND.
Read the whole link and you'll figure it out.
well, i saw your given link... much of the content just bounced off my head.. can u please expain me in simple terms...
I will post another link here: if you don't understand from this one, I'll try to exaplin.
Bitwise operations in C wiki
Bitwise operations wiki
Ok, so...
AND truth table
This is the table
0 & 0 = 0
1 & 0 = 0
0 & 1 = 0
1 & 1 = 1
This is the truth table of the AND bitwise operator. In programming language every single thing is transalted into bytes, and smaller bits.
1 byte=8 bits.
Here are some info about bits: http://en.wikipedia.org/wiki/Bit
So, in your case you had a for from 0 to 9, all of that bitwise and with 1 (i & 1).
First of all we will translate every single i into binary:
Decimal Bianry
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
Ok so, we have the numbers into binary, and we have the AND bitwise table.
We now have to bitwise and them together.
so we start with 0 & 1:
We have 0 & 1:
0 0 0 0 &
0 0 0 1
-------
0 0 0 0
//You take from the table every single digit.
//So first: it's from right to left: 0 with 1 from the table is 0, then 0 & 0 is zero. So the
//answer is 0 0 0 0.
We have 1 & 1:
0 0 0 1 &
0 0 0 1
-------
0 0 0 1
//0001 in binary if you check the table is 1, so the answer is 1.
We take 2 & 1:
0 0 1 0 &
0 0 0 1
-------
0 0 0 0
//0000 in binary is 0.
//don't forget, you have to check every single pair digit, so 0 & 1 is 0, 1 & 0 is 0 etc.
And you do that with every single number.
Hope I made myself clear:D
For example, if you bitwise & 6 with 13 it will give you 4:
6: 0110
13: 1100
0 1 1 0 &
1 1 0 0
-------
0 1 0 0
//which is 4 from the conversion table.
Thanks a lot dude.... its clear now....