[Programming] Question 12: Demonstrate multiplication by powers of 2 using left shift operator in C?

 

#include <stdio.h>

int main() {
	int num = 5;

	printf("Original number: %d\n", num);

	num = num << 3; // Multiply by 2^3 = 8

	printf("After multiplication by 8: %d\n", num);

	return 0;
} 

Inside the main() function:

- An integer variable num is initialized with the value 5.

- The original value of num is printed to the console.

- The num variable is left shifted by 3 bits using the `<<` operator, effectively multiplying it by 8 (2^3).

- The result after multiplication is printed to the console.



Featured Posts

Leetcode 4. Median of Two Sorted Arrays

  4. Median of Two Sorted Arrays Hard Given two sorted arrays  nums1  and  nums2  of size  m  and  n  respectively, return  the median  of t...

Popular Posts