Skip to content

Instantly share code, notes, and snippets.

@false-git
Created February 11, 2017 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save false-git/6f1c3e4b764ce2a0fb74e052f819ab46 to your computer and use it in GitHub Desktop.
Save false-git/6f1c3e4b764ce2a0fb74e052f819ab46 to your computer and use it in GitHub Desktop.
偶数パリティの計算
#include <stdio.h>
#include <stdint.h>
/*!
* \brief 7bitの数値を渡すと、偶数パリティを8bit目に入れる
* \param[in] val 7bitの数値
* \return 8bit目に偶数パリティを追加したもの
*/
uint32_t parity(uint32_t val) {
uint32_t orig = (val &= 0x7f);
val ^= val << 4;
val ^= val << 2;
val ^= val << 1;
return (val & 0x80) | orig;
}
int main(int argc, char *argv[]) {
for (uint32_t i = 0; i < 128; i++) {
printf("%02x:%02x\n", i, parity(i));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment