Loading lang_c_ex_0901...
~,
|,
&,
^,
<<,
>>(ainsi que les variantes avec
=).
(v&(1u<<N))!=0).
v|=(1u<<N);).
v&=~(1u<<N);).
uint32_t bitVote(uint32_t word1, uint32_t word2, uint32_t word3) { // ... }
void test_bitVote(void) { printf("\n~~~~ %s() ~~~~\n", __func__); // ... }
uint32_t bitMirror(uint32_t word) { // ... }
void test_bitMirror(void) { printf("\n~~~~ %s() ~~~~\n", __func__); // ... }
uint32_t rotateLeft(uint32_t word) { // ... }
void test_rotateLeft(void) { printf("\n~~~~ %s() ~~~~\n", __func__); // ... }
uint32_t rotateRight(uint32_t word) { // ... }
void test_rotateRight(void) { printf("\n~~~~ %s() ~~~~\n", __func__); // ... }
//----------------------------------------------------------------------------
#ifndef BITWISE_H #define BITWISE_H
#include <stdint.h>
uint32_t bitVote(uint32_t word1, uint32_t word2, uint32_t word3);
uint32_t bitMirror(uint32_t word);
uint32_t rotateLeft(uint32_t word);
uint32_t rotateRight(uint32_t word);
#endif // BITWISE_H
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "bitwise.h"
uint32_t bitVote(uint32_t word1, uint32_t word2, uint32_t word3) { uint32_t result=0; for(uint32_t b=0; b<32; ++b) { const uint32_t mask=1u<<b; int count=0; if(word1&mask) { ++count; } if(word2&mask) { ++count; } if(word3&mask) { ++count; } if(count>=2) { result|=mask; } } return result; }
uint32_t bitMirror(uint32_t word) { uint32_t result=0; for(uint32_t b=0; b<32; ++b) { if(word&(1u<<b)) { result|=(1u<<(31-b)); } } return result; }
uint32_t rotateLeft(uint32_t word) { const uint32_t bit=(word>>31)&1u; return (word<<1)|bit; }
uint32_t rotateRight(uint32_t word) { const uint32_t bit=(word&1u)<<31; return (word>>1)|bit; }
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "bitwise.h"
#include <stdio.h>
void test_bitVote(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const uint32_t word1=5, word2=3, word3=14; printf("vote of %u, %u and %u gives %u\n", word1, word2, word3, bitVote(word1, word2, word3)); const uint32_t word4=15, word5=204, word6=170; printf("vote of %u, %u and %u gives %u\n", word4, word5, word6, bitVote(word4, word5, word6)); }
void test_bitMirror(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const uint32_t words[]={(1u<<0), (1u<<1), (1u<<8), (1u<<8)|(1u<<0), (1u<<8)|(1u<<1), (1u<<31), (1u<<30), (1u<<23), (1u<<23)|(1u<<31), (1u<<23)|(1u<<30)}; const int count=(int)(sizeof(words)/sizeof(words[0])); for(int i=0; i<count; ++i) { const uint32_t word=words[i]; printf("mirror of %u gives %u\n", word, bitMirror(word)); } }
void test_rotateLeft(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const uint32_t words[]={(1u<<0), (1u<<1), (1u<<8), (1u<<8)|(1u<<0), (1u<<8)|(1u<<1), (1u<<31), (1u<<30), (1u<<23), (1u<<23)|(1u<<31), (1u<<23)|(1u<<30)}; const int count=(int)(sizeof(words)/sizeof(words[0])); for(int i=0; i<count; ++i) { const uint32_t word=words[i]; printf("left rotation of %u gives %u\n", word, rotateLeft(word)); } }
void test_rotateRight(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const uint32_t words[]={(1u<<0), (1u<<1), (1u<<8), (1u<<8)|(1u<<0), (1u<<8)|(1u<<1), (1u<<31), (1u<<30), (1u<<23), (1u<<23)|(1u<<31), (1u<<23)|(1u<<30)}; const int count=(int)(sizeof(words)/sizeof(words[0])); for(int i=0; i<count; ++i) { const uint32_t word=words[i]; printf("right rotation of %u gives %u\n", word, rotateRight(word)); } }
int main(void) { test_bitVote(); test_bitMirror(); test_rotateLeft(); test_rotateRight(); return 0; }
//----------------------------------------------------------------------------