C Programming - Bitwise Operators - Discussion
Discussion Forum : Bitwise Operators - Find Output of Program (Q.No. 8)
8.
What will be the output of the program?
#define P printf("%d\n", -1^~0);
#define M(P) int main()\
{\
P\
return 0;\
}
M(P)
Discussion:
27 comments Page 1 of 3.
NJ Nath said:
5 years ago
^ is Bitwise XOR operator, perform -1 XOR -1, the answer will be 0000.
(3)
Laxman said:
6 years ago
Let you take int i=1;
int is 2-byte range in turbo c, 4 bytes in gcc, now consider turbo c==>2 bytes means 16 bits right so these 16 bits stored in memory for 1 is (0000 0000 0000 0001). Before discoursing about -1; what is 2's complement of x, that is exactly equal to -x. note: negative numbers stored in the memory is 2's comp of a positive number. so we have to find what is 2's comp of 1. 2's comp=1'comp+1;
0000 0000 0000 0001==>1'comp is==>1111 1111 1111 1110 ==>add 1==>1111 1111 1111 1111. So finally the -1 is stored in memory is 1111 1111 1111 1111.
int is 2-byte range in turbo c, 4 bytes in gcc, now consider turbo c==>2 bytes means 16 bits right so these 16 bits stored in memory for 1 is (0000 0000 0000 0001). Before discoursing about -1; what is 2's complement of x, that is exactly equal to -x. note: negative numbers stored in the memory is 2's comp of a positive number. so we have to find what is 2's comp of 1. 2's comp=1'comp+1;
0000 0000 0000 0001==>1'comp is==>1111 1111 1111 1110 ==>add 1==>1111 1111 1111 1111. So finally the -1 is stored in memory is 1111 1111 1111 1111.
Yasaswini said:
7 years ago
Can anyone give the representation of 1 and -1 in binary digits?
Ramesharavind said:
7 years ago
@Vinod.
Your explanation is good, Thanks.
Your explanation is good, Thanks.
Xyz said:
7 years ago
Please explain me why \ is used?
Rakesh said:
8 years ago
Answer = 0 because,
Binary of -1(in 2 byte) 1111 1111 1111 1111.
Binary of ~0(in 2 byte) 1111 1111 1111 1111.
Now, apply Xor (-1 ^ ~0) = 0000 0000 0000 0000(ans).
Hint:use Xor truth table.
Binary of -1(in 2 byte) 1111 1111 1111 1111.
Binary of ~0(in 2 byte) 1111 1111 1111 1111.
Now, apply Xor (-1 ^ ~0) = 0000 0000 0000 0000(ans).
Hint:use Xor truth table.
(2)
Hareesh said:
10 years ago
main() is function first it will call , after that printf will work.
Note : Printf function you can write in inside main function.
Like main(printf("hai"))
If anybody ask Printf function how can write without ends with semicolon means this is solution.
Note : Printf function you can write in inside main function.
Like main(printf("hai"))
If anybody ask Printf function how can write without ends with semicolon means this is solution.
(1)
Yogesh kumar said:
1 decade ago
Can anyone ans me how the execution is reaching to PRINTF block as main is defined after it?
Madhav said:
1 decade ago
BY using \ we can use multiple line macro, it indicates that first line is continued.
Abcd said:
1 decade ago
Please explain multiple macros how to split?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers