Skip to content

Instantly share code, notes, and snippets.

@mleko
Created April 7, 2017 11:19
Show Gist options
  • Save mleko/0a2028d21d7400bc5e97a6f56a06db6f to your computer and use it in GitHub Desktop.
Save mleko/0a2028d21d7400bc5e97a6f56a06db6f to your computer and use it in GitHub Desktop.
Binary to Gray code (and reverse) conversion functions https://en.wikipedia.org/wiki/Gray_code
/**
The MIT License (MIT)
Copyright (c) 2017 Daniel Król
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* Converse from binary to Gray code */
int binary_to_gray(int b) {
return ((b >> 1) ^ b);
};
/* Converse from Gray code to binary */
int gray_to_binary(int g) {
int h = g;
while(h) {
h >>= 1;
g ^= h;
}
return g;
};
/**
The MIT License (MIT)
Copyright (c) 2017 Daniel Król
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* Converse from binary to Gray code */
int binary_to_gray(int b);
/* Converse from Gray code to binary */
int gray_to_binary(int g);
/**
The MIT License (MIT)
Copyright (c) 2017 Daniel Król
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include "gray_binary.h"
void assert(int ok, char *str);
char buffer[100];
void check(int binary, int gray){
int a;
a = binary_to_gray(binary);
snprintf(buffer, sizeof buffer, "B2G %d!=%d", a, gray);
assert(a == gray, buffer);
a = gray_to_binary(gray);
snprintf(buffer, sizeof buffer, "G2B %d!=%d", a, binary);
assert(a == binary, buffer);
}
void testSymmetry() {
for(int i = 0 ; i < 17 ; i++){
snprintf(buffer, sizeof buffer, "symmetry %d", i);
assert(binary_to_gray(gray_to_binary(i)) == i, buffer);
}
}
void test() {
testSymmetry();
check(0, 0);
check(3, 0b10);
check(6, 0b101);
check(7, 0b100);
};
int result = 0;
int assertCount = 0;
int main() {
test();
if(!result) {
printf("Tests passed. Assert count: %d\n", assertCount);
}
return result;
}
void assert(int ok, char *str) {
assertCount++;
if(!ok) {
printf("Failed assert: %s\n", str);
result = 1;
}
}
#!/bin/sh
tmpfile=$(mktemp /tmp/gray_binary_XXXXXXX.test)
gcc gray_binary.c test.c -o $tmpfile
$tmpfile
rm $tmpfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment