+ 4
Help me in understanding this
fruitx= rand() &! width; fruity= rand() &! height; ------------------------------------------- -while fruitx is an integer variable and fruitx is also an integer variable -(width) is a constant variable assign the value of 20 so also (height)
1 Answer
+ 1
Hi DUDE,
After executing both lines:
fruitx= rand() &! width;
fruity= rand() &! height;
We will obtain as result that: fruitx = fruity = 0;
And the reason is the bitwise operator "!" that means "negation" together with bitwise operator "&" or AND.
So, in C any non-zero int variable to which we apply the operator "!" becomes zero. Conversely, if the variable is zero, it becomes 1;
This comes from boolean logic:
!True = False
!False = True
However, in C, an int variable is seem as "true" if its value is not zero, otherwise it is zero.
So, in "fruitx= rand() &! width" you are taking a positive variable "width = 20" which means "true" and "negating" it, thus forcing it to zero (or "false"). Then you are doing a bitwise AND between the value returned by rand() and "0". From the Boolean logic:
x AND 0 = 0. (The value of x does not matter)
Any other doubts?