/*Write a C program that takes a hexadecimal number and an integer 'pos' as input.
Set 'pos' number of bits from the right to 1 in the given hexadecimal number
and print the modified hexadecimal number as the output.*/
#include <stdio.h>
int set_all_right_bits(int x, int pos) {
for (int i = 0; i < pos; i++) {
x = x | (1 << i);//0001 << 1-4 so all becomes 1111 | makes 0,1 to 1
}
return x;
}
int main() {
int a;//0xaa55
int pos;//4
printf("Enter a hexadecimal number: ");
scanf("%x", &a);
printf("Enter the number of bits to set from the right: ");
scanf("%d", &pos);
printf("Input: 0x%x, pos = %d\n", a, pos);
int out = set_all_right_bits(a, pos);
printf("Output: 0x%x\n", out);
return 0;
}
To embed this program on your website, copy the following code and paste it into your website's HTML: