#include <stdio.h>
void printhex(int hex) {
int digits[8]; // Assuming the maximum digits needed for a 32-bit integer
int i = 0;
// Extract hexadecimal digits
while (hex != 0) {
digits[i] = hex & 0xF; // Extract the last 4 bits (hexadecimal digit)
hex = hex >> 4; // Shift right by 4 bits
i++;
}
// Print the digits in reverse order
for (int j = i; j >= 0; j--) {
printf("%x", digits[j]);
}
}
int main() {
int hex = 0x0000;
int x = hex;
int count = 0;
// Count the number of bits set to 1 in the hex value
while (hex != 0) {
if ((hex & 0b1) == 1) { // Check the least significant bit
count++;
}
hex = hex >> 1; // Shift right by 1 bit (for binary bit checking)
}
printhex(x); // Print the hexadecimal value
printf("\n");
int mask = 1 << 4; // Shifts 1 to the left by 3 bits, which results in 0b00001000
x = x | (0b0001000000000001);
printhex(x);
printf("\n");
x = x | mask;
printhex(x);
printf("\n");
printf("\n%d", count); // Print the count of 1 bits
return 0;
}
To embed this program on your website, copy the following code and paste it into your website's HTML: