#include <stdio.h>
int main() {
int a = 0b101010;
char binaryRepresentation[7]; // 6 bits + 1 for null terminator
// Fill the binaryRepresentation array with binary digits
for (int i = 5; i >= 0; i--) {
binaryRepresentation[5 - i] = ((a >> i) & 1) + '0'; // Convert to ASCII
}
binaryRepresentation[6] = '\0'; // Null terminator
// Print the binary representation
printf("Binary representation: 0b%s\n", binaryRepresentation);
return 0;
}
/*
#include <stdio.h>
int main() {
int hexValues[] = {0x96A4, 0x123, 0xABCD};
for (int i = 0; i < sizeof(hexValues) / sizeof(hexValues[0]); i++) {
printf("Hexadecimal value[%d]: 0x%X\n", i, hexValues[i]);
}
return 0;
}
The addition of '0' in the expression (a >> i) & 1) + '0' is used to convert the binary digit (0 or 1)
to its ASCII representation. In C, the ASCII value for the character '0' is 48, and for the character
'1' is 49.
Here's a breakdown of the expression:
(a >> i) & 1: Extracts the i-th bit of the integer a.
+ '0': Adds the ASCII value of '0' to the result.
This addition effectively converts the numeric value (0 or 1) to its ASCII character representation.
If the result is 0, adding '0' converts it to the ASCII character '0', and if the result is 1, adding
'0' converts it to the ASCII character '1'.
In C, when you print a character using %c format specifier, it interprets the value as an ASCII character.
Therefore, adding '0' is a common idiom to convert a numeric digit to its character representation in
ASCII.
So, (a >> i) & 1) + '0' is used to convert the binary digit to its ASCII character representation in the
array binaryRepresentation.
*/
To embed this program on your website, copy the following code and paste it into your website's HTML: