Software Development Fundamentals (SDF) – I
ODD 2023
Lecture 7
Practice Questions on Operators:
1. Write a program to swap two numbers without using a temporary variable. (Hint: Use arithmetic
operators)
2. Write a program to check if a number is a power of 2 using bitwise operators.
3. Write a program to find the smallest of three numbers using the conditional operator.
4. Write a program to check if the given number is perfect.
5. Write a program to print if a number is even or odd without using any arithmetic or relational
operator.
6. Write a program that multiplies a number by 8 using bitwise operators.
7. Given the code snippet, what will it print?
8. Set n-th bit of a number using bitwise operators
9. Clear n-th bit of a number using bitwise operators
10. Toggle n-th bit of a number using bitwise operators
11. Check if a number is negative using bitwise operators
1. Solution using Arithmetic Operators: Solution using Bitwise Operators:
#include <stdio.h> #include <stdio.h>
int main() { int main() {
int a = 5, b = 7; int a = 5, b = 7;
printf("Before swapping: a = %d, b = %d\n", a, b); printf("Before swapping: a = %d, b = %d\n", a, b);
a = a + b; a = a ^ b;
b = a - b; // This now becomes (a + b) - b = a b = a ^ b; // This now becomes (a ^ b) ^ b = a
a = a - b; // This now becomes (a + b) - a = b a = a ^ b; // This now becomes (a ^ b) ^ a = b
printf("After swapping: a = %d, b = %d\n", a, b);
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
return 0;
}
}
2.
#include <stdio.h>
int main() {
int num = 16;
// A number is a power of 2 if it has only one '1' bit in binary representation.
// num & (num - 1) will be zero for power of 2 numbers.
if (num > 0 && (num & (num - 1)) == 0) {
printf("%d is a power of 2.\n", num);
} else {
printf("%d is not a power of 2.\n", num);
}
return 0;
}
3.
#include <stdio.h>
int main() {
int a = 10, b = 5, c = 8;
int smallest = (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c);
printf("The smallest number among %d, %d, and %d is: %d\n", a, b, c, smallest);
return 0;
}
4. A perfect number is a positive integer that is equal to the sum of its proper divisors, excluding itself. For
example, 28 is a perfect number because its divisors are 1, 2, 4, 7, 14, and the sum of these values is 28. Write
a program to check if the given number is perfect.
#include <stdio.h> if (sum == num) {
int main() {
printf("%d is a perfect
int num, sum = 0;
number.\n", num);
printf("Enter a number: ");
scanf("%d", &num);
} else {
printf("%d is not a perfect
for (int i = 1; i <= num / 2; i++) { number.\n", num);
if (num % i == 0) { }
sum += i;
} return 0;
} }
5. Write a program to print if a number is even or odd without using
any arithmetic or relational operator.
#include <stdio.h>
The bitwise & operator is
int main() { used to check the last bit of
the integer. If it's 1, the
int num = 4; number is odd, otherwise
even.
(num & 1) ? printf("Odd\n") : printf("Even\n");
return 0;
}
6. Write a program that multiplies a number by 8 using bitwise
operators.
#include <stdio.h>
int main() {
int num = 4;
int result = num << 3;
printf("Number after multiplication by 8: %d\n", result);
return 0;
}
Note: Left shifting a number by n bits means multiplying it by 2^n. So,
left shifting by 3 bits multiplies the number by 8.
7. Given the following code snippet, what will it print?
int main() {
int i = 1, j = 2, k = 3;
printf("%d", (i, j, k));
return 0;
}
Solution:
The comma operator evaluates its first operand and discards the result,
then evaluates the second operand and returns its value. In the given
code, i, j, and k are evaluated in order, but only the value of k (which is
3) is returned and printed. So, the program will print 3.
8. Set n-th bit of a number
#include <stdio.h>
int main() {
The line num |= (1 << n);
int num, n; sets the n-th bit to 1.
printf("Enter an integer: "); 1 << n shifts the bit 1 to the
left by n places.
scanf("%d", &num); The |= operator then
performs a bitwise OR
printf("Enter the bit position (0-based) to set: "); operation with the original
scanf("%d", &n); number and the shifted
value, which effectively
num |= (1 << n); sets the n-th bit.
printf("Number after setting %d-th bit: %d\n", n, num);
return 0;
}
9. Clear n-th bit of a number
This program clears (sets to
#include <stdio.h> 0) the n-th bit of an integer.
The line num &= ~(1 << n);
int main() { clears the n-th bit.
int num, n; 1 << n again shifts the bit 1 to
the left by n places.
printf("Enter an integer: "); ~(1 << n) then inverts all the
scanf("%d", &num); bits, making our desired bit 0
and all others 1.
printf("Enter the bit position (0-based) to clear: "); The &= operator performs a
bitwise AND operation with
scanf("%d", &n); the original number and the
num &= ~(1 << n); inverted shifted value, which
clears the n-th bit.
printf("Number after clearing %d-th bit: %d\n", n, num);
return 0;
}
10. Toggle n-th bit of a number This program toggles the n-th bit of an
#include <stdio.h> integer. If it's 1, it becomes 0, and
vice-versa.
int main() { The line num ^= (1 << n); toggles the
int num, n; n-th bit.
1 << n does the usual left shift.
printf("Enter an integer: "); The ^= operator is a bitwise XOR
operation. If the n-th bit in the original
scanf("%d", &num); number is 1, it'll be set to 0, and if it's 0,
it'll be set to 1.
printf("Enter the bit position (0-based) to toggle: ");
scanf("%d", &n);
num ^= (1 << n);
printf("Number after toggling %d-th bit: %d\n", n,
num);
return 0;
}
11. Check if a number is negative using bitwise operators
#include <stdio.h>
int main() { This program checks if a given integer
int num; is negative by examining its most
printf("Enter an integer: "); significant bit (the sign bit in Two's
complement representation).
scanf("%d", &num); In two's complement representation,
if (num & (1 << (sizeof(int) * 8 - 1))) { the most significant bit (MSB)
represents the sign of the number. If
printf("The number is negative.\n"); it's 1, the number is negative; if it's 0,
} else { the number is non-negative.
printf("The number is positive or zero.\n"); sizeof(int) * 8 - 1 calculates the
position of the MSB (0-based).
} The line if (num & (1 << (sizeof(int) * 8
- 1))) checks if the MSB is 1.
return 0;
}
End of
Lecture 7