ProgrammingModerateTCS NQT · Infosys
The binary-reflected Gray code of i is g(i) = i ^ (i >> 1). What is g(4)?
- A. 12
- B. 2
- C. 6
- D. 7
Correct answer: C. 6
4 is 100 and 4 >> 1 is 010, so the XOR is 110 = 6. 12 shifts left instead of right, 2 forgets the XOR, and 7 applies the Gray decoding (i ^ i>>1 ^ i>>2) instead of the encoding.
More C Programming questions
- What is popcount(0xFFFF0000 ^ 0x0F0F0F0F)?
- int q = NULL; if (q != NULL && q == 3) printf("yes"); else printf("no"); What is printed?
- #define ADD(a,b) a+b. What does printf("%d", 2 ADD(1,2) 3) print?
- #define HALF(x) x / 2. With int a = 5, b = 3, what does printf("%d", HALF(a + b)) print?
- char s[] = "2024"; int sum = 0; for (int i = 0; s[i] != '0'; i++) sum += s[i] - '0'; What is sum?
- int count = 0; for (int i = 1; i <= 10; i++) for (int j = i; j <= 10; j += i) count++; What is count at the end?