/*
794 名前:デフォルトの名無しさん[sage] 投稿日:2014/05/26(月) 07:45:41.42 ID:ptXAqcmg [2/4]
お題: ビット演算を四則演算のみで実装せよ。ただし、ループも再帰も使ってはならない。
int など整数が固定長の言語でお願いします。
結構難しいかも。
上級者の人は、どうやったら計算量を落とせるかも考えてくれるとうれしい。
*/
#include <stdio.h>
unsigned int not(unsigned int a) {
return -1 - a;
}
unsigned int and(unsigned int a, unsigned int b) {
unsigned int c = 1;
#define ha (a -= (a/c * (b/c + 1) % 2) * c), (c += c)
#define HA ha, ha, ha, ha, ha, ha, ha, ha
return HA, HA, HA, HA, a;
}
unsigned int or(unsigned int a, unsigned int b) {
return not(and(not(a), not(b)));
}
unsigned int xor(unsigned int a, unsigned int b) {
return and(or(a, b), not(and(a, b)));
}
int main(void) {
int i, ss[][2] = {{0x1a,0x16},{123456,777},{-1,-1}};
for (i = 0; i < (int)(sizeof(ss)/sizeof(ss[0])); i++) {
int a = ss[i][0], b = ss[i][1];
printf(" ~a =0x%08X not(a) =0x%08X\n", ~a
, not
(a
)); printf(" a&b=0x%08X and(a,b)=0x%08X\n", a
&b
, and
(a
,b
)); printf(" a|b=0x%08X or(a,b) =0x%08X\n", a
|b
, or
(a
,b
)); printf(" a^b=0x%08X or(a,b) =0x%08X\n", a
^b
, xor
(a
,b
)); }
return 0;
}
LyoKNzk0IOWQjeWJje+8muODh+ODleOCqeODq+ODiOOBruWQjeeEoeOBl+OBleOCk1tzYWdlXSDmipXnqL/ml6XvvJoyMDE0LzA1LzI2KOaciCkgMDc6NDU6NDEuNDIgSUQ6cHRYQXFjbWcgWzIvNF0K44GK6aGMOiDjg5Pjg4Pjg4jmvJTnrpfjgpLlm5vliYfmvJTnrpfjga7jgb/jgaflrp/oo4XjgZvjgojjgILjgZ/jgaDjgZfjgIHjg6vjg7zjg5fjgoLlho3luLDjgoLkvb/jgaPjgabjga/jgarjgonjgarjgYTjgIIKaW50IOOBquOBqeaVtOaVsOOBjOWbuuWumumVt+OBruiogO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