Skip to content

Instantly share code, notes, and snippets.

@ympons
Created August 2, 2016 21:09
Show Gist options
  • Save ympons/0a59e9dcaf2551713a63c9aa7ba361b9 to your computer and use it in GitHub Desktop.
Save ympons/0a59e9dcaf2551713a63c9aa7ba361b9 to your computer and use it in GitHub Desktop.
Check if the bits in a char are a palindrome
#include <stdio.h>
#include <limits.h>
int isPalind(char c) {
int a[CHAR_BIT];
for(int i=0;i<CHAR_BIT;i++) {
a[i] = (c >> i) & 1;
}
for (int i=0;i<5;i++) {
if (a[i] != a[7-i]) {
return -1;
}
}
return 1;
}
int main(void) {
if (isPalind(0xA5)!=-1) {
printf("%s", "YES");
} else {
printf("%s", "NO");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment