ProgrammingHardCompetitive coding (CP)
How many numbers from 1 to 100 have bit 3 set (value 8)? Compute with this program.
How many numbers from 1 to 100 have bit 3 set (value 8)? Compute with this program.
#include <stdio.h>
int main(void) {
int c = 0;
for (unsigned n = 1; n <= 100; n++)
if (n >> 3 & 1u) c++;
printf("%d", c);
}- A. 50
- B. 49
- C. 48
- D. 52
Correct answer: C. 48
Bit 3 is set in blocks of 8 numbers that alternate with blocks of 8 that are clear, so each 16-number period contributes 8 and 1 to 100 gives 48.
More C Programming questions
- What is printed? Assume uint16t is 16 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint16t x = 60000; uint16t y = x 2…
- How many ways are there to make 10 from unlimited coins of value 2, 3 and 5? Compute with this program. c #include <stdio.h> int ways(int…
- What is the length of the longest common subsequence of ABCBDAB and BDCABA? The program computes it by plain recursion without a table…
- How many islands does this 4-connected flood-fill count find in the 3 by 3 grid "101", "010", "101"? Compute with the program. c #include…
- How many of these statements are true when x = 255? Count them with this program. c #include <stdint.h> #include <stdio.h> int main(void)…
- What is printed? c #include <stdio.h> int main(void) { unsigned a = 3; int b = -5; printf("%d", (a + b) > 0); }