Bitwise Operators Questions
Practice Bitwise Operators MCQs with answers and explanations. Page 1 of 2.
Category
C Programming
Topic
Bitwise Operators
Page
1 / 2
Mode
Practice
Questions
Open any question to view the answer and explanation.
Assume 16-bit signed int: what hexadecimal is printed for this arithmetic right shift?
#include<stdio.h>
int main()
{
printf("%x
", -1 >> 1);
return 0;
}
Open
View answer
Bit masking and shifts in C: evaluate the expression and print the result.
#include<stdio.h>
int main()
{
unsigned int res;
res = (64 >>(2 + 1 - 2)) & (~(1 << 2));
printf("%d
", res);
return 0;
}
Open
View answer
ASCII and bitwise OR on character codes: what five characters are printed?
#include<stdio.h>
int main()
{
char c = 48; // ASCII '0'
int i, mask = 01; // octal 1
for (i = 1; i <= 5; i++)
{
printf("%c", c | mask);
mask = mask << 1;
}
return 0;
}
Open
View answer
Operator precedence with bitwise and logical operators: predict the three integers printed.
#include<stdio.h>
int main()
{
int i = 4, j = 8;
printf("%d, %d, %d
", i | j & j | i, i | j && j | i, i ^ j);
return 0;
}
Open
View answer
Bitwise OR, AND, and XOR with identical values in decimal and hex: compute and print all results.
#include<stdio.h>
int main()
{
int i = 32, j = 0x20;
int k, l, m;
k = i | j;
l = i & j;
m = k ^ l;
printf("%d, %d, %d, %d, %d
", i, j, k, l, m);
return 0;
}
Open
View answer
In C programming (assuming unsigned int is 2 bytes = 16 bits), what will this program print in hexadecimal?
#include<stdio.h>
int main()
{
unsigned int a = 0xffff; // 16-bit all 1s
~a; // bitwise NOT result is not stored back
printf("%x
", a);
return 0;
}
Choose the exact output produced by printf.
Open
View answer
In C (unsigned int is 2 bytes = 16 bits), evaluate the effect of bitwise NOT on 32 and determine the printed hexadecimal value:
#include<stdio.h>
int main()
{
unsigned int m = 32; // 0x0020 on 16 bits
printf("%x
", ~m);
return 0;
}
Select the exact output.
Open
View answer
In C, consider missing arguments to printf: what will actually be printed (conceptually) by this code using bit shifts?
#include<stdio.h>
int main()
{
printf("%d >> %d %d >> %d
", 4 >> 1, 8 >> 1);
return 0;
}
Note: The format string has four %d placeholders but only two arguments.
Open
View answer
Evaluate this macro-based C program: what integer does it print?
#define P printf("%d
", -1 ^ ~0)
#define M(P) int main()\
{\
P\
return 0;\
}
M(P)
Assume typical two’s-complement semantics.
Open
View answer
Bit shifts with negative counts in C: determine the safest conclusion about this program’s outputs (assume typical hosted C, int is 32 bits).
#include<stdio.h>
int main()
{
printf("%d %d
", 32<<1, 32<<0);
printf("%d %d
", 32<<-1, 32<<-0);
printf("%d %d
", 32>>1, 32>>0);
printf("%d %d
", 32>>-1, 32>>-0);
return 0;
}
Note: shifting by a negative amount is not defined by the C standard.
Open
View answer
Assuming a 2-byte (16-bit) int in C, what hexadecimal value is printed here?
#include<stdio.h>
int main()
{
printf("%x
", -1 << 3);
return 0;
}
Pick the most appropriate result given typical two’s-complement behavior.
Open
View answer
Given unsigned char i = 0x80 in C, what decimal value is printed?
#include<stdio.h>
int main()
{
unsigned char i = 0x80; // 128
printf("%d
", i << 1);
return 0;
}
Assume usual integer promotions apply before the shift.
Open
View answer
Concept check: In C, are the bitwise operators & and | unary operators or binary (dyadic) operators?
Open
View answer
Reasoning about left shifts: Is left-shifting an integer by 1 always equivalent to multiplying it by 2 in C?
Answer considering overflow and signedness rules.
Open
View answer
Bitwise AND for division: Can the bitwise & operator be used to divide an integer by powers of 2 in C?
Choose the correct statement.
Open
View answer
In C/C++, can the bitwise AND operator (&) be used to test whether more than one bit is set in an integer?
Give a precise, programming-focused answer and consider common idioms such as x & (x - 1) to detect if multiple bits are on.
Open
View answer
Right shifting a negative (signed) integer: does the result vary across systems when using expression1 >> expression2 if the sign bit is 1?
Open
View answer
Using bitwise AND (&) with a mask to test whether a specific bit is set in an integer: is this a valid and standard technique?
Open
View answer
Is left shifting an unsigned int or unsigned char by 1 always equivalent to multiplying by 2 (in practical C/C++ code)?
Open
View answer
When you left shift an integer in C/C++, do the leftmost bits “rotate” around to the right side (i.e., is a left shift a rotate)?
Open
View answer
Practice smarter
Solve a few questions daily and revisit weak topics regularly to improve accuracy.