|
LiveCode 論理演算ノート
|
index@kenjikojima.com |
|
|
|
||
|
|
||
10進数 <-> 2進数 変換:LiveCodeのシンタックス baseConvert(変換する数,変換する数の元の進数(originalBase),変換させる先の進数(destinationBase))
AND演算 bitAnd
:両方が1なら1。それ以外は0。「論理積」
1 and 1 -> 1 0 and 1 -> 0 1 and 0 -> 0 0 and 0 -> 0 LiveCodeでの演算 バイナリーの演算 bitAnd のシンタックス:number1 bitAnd number2 10101001 (binary 1) baseConvert(10101001,2,10) -> 169 (10進数) 11110000 (binary 2) baseConvert(11110000,2,10) -> 240 (10進数) ------------------------ 10100000 (the result) 169 bitAnd 240 -> 160 baseConvert(160,10,2) -> 10100000 LiveCodeのrepeatを使ったbitAnd repeat with i= the num of chars of binary1 down to 1 if char i of binary1 is 1 and char i of binary2 is 1 then put 1 before tBit else put 0 before tBit end if end repeat
OR演算 bitOr
:どちらかが1なら1。それ以外は0。「論理和」
1 and 1 -> 1 0 and 1 -> 1 1 and 0 -> 1 0 and 0 -> 0 LiveCodeでの演算 バイナリーの演算 bitOr のシンタックス:number1 bitOr number2 10101001 (binary 1) baseConvert(10101001,2,10) -> 169 (10進数) 11110000 (binary 2) baseConvert(11110000,2,10) -> 240 (10進数) ------------------------ 11111001 (the result) 169 bitOr 240 -> 249 baseConvert(249,10,2) -> 11111001 LiveCodeのrepeatを使ったbitOr repeat with i= the num of chars of binary1 down to 1 if char i of binary1 is 0 and char i of binary2 is 0 then put 0 before tBit else put 1 before tBit end if end repeat
XOR演算 bitXor :両方が同じなら0。どちらかが違っていれば1。「排他的論理和」
1 and 1 -> 0 0 and 1 -> 1 1 and 0 -> 1 0 and 0 -> 0 XOR演算は視聴覚の分離と融合でワンタイム・パッドで使用しました。 LiveCodeでの演算 バイナリーの演算 bitXor のシンタックス:number1 bitXor number2 10101001 (binary 1) baseConvert(10101001,2,10) -> 169 (10進数) 11110000 (binary 2) baseConvert(11110000,2,10) -> 240 (10進数) ------------------------ 01011001 (the result) 169 bitOr 240 -> 89 baseConvert(89,10,2) -> 1011001 7桁を返す format("%08s",baseConvert(89,10,2)) -> 01011001 format(%[0 & charLength]s,数値) は桁数を揃えるファンクション LiveCodeのrepeatを使ったbitXor repeat with i= the num of chars of binary1 down to 1 if char i of binary1 is char i of binary2 then put 0 before tBit else put 1 before tBit end if end repeat
NOT演算 bitNot
:1つの値が反転する演算をする。1ならば0。0ならば1。「論理否定」
1 -> 0
0 -> 1
1 -> 0
0 -> 1
LiveCodeでの演算
バイナリーの演算 bitNot のシンタックス:bitNot number
10101001 (binary) baseConvert(10101001,2,10) -> 169 (10進数)
------------------------ bitNot 169 -> 4294967126 (結果はバグと思われます)
01010110 (the result) 10進数では baseConvert(01010110,2,10) -> 86にならなくてはいけない
bitNot 1 -> 0 とならなくてはいけないのですが 4294967294 を返します
bitNotのバグ修正するファンクション
-- 10進数からNOT演算の10進数を返す。パラメータ「pBase10Num」は10進数
function bugFixedBitNot pBase10Num
put baseConvert(pBase10Num,10,2) into tBinary
repeat for each char tChar in tBinary
if tChar is 0 then put 1 after binNum; else put 0 after binNum
end repeat
return baseConvert(binNum,2,10)
end bugFixedBitNot
バグ修正を使って10進数「169」をNOT演算(bitNot)する
bugFixedBitNot(169) -> 86 (結果)
format("%08s",baseConvert(86,10,2)) -> 01010110
bugFixedBitNot(1) -> 0 を返します。
しかし10進数でNOT演算をする意味があるかは、今はここで触れません。
LiveCodeのrepeatを使ったbitNot
repeat with i= the num of chars of binary1 down to 1
if char i of binary1 is 1 then
put 0 before tBit
else
put 1 before tBit
end if
end repeat
バイナリーの加算(論理演算を使用しない)
00001010 (binary a) baseConvert(00001010,2,10) -> 10 (10進数)
00001100 (binary b) baseConvert(00001100,2,10) -> 12 (10進数)
------------------------
00010110 (a+bの結果) baseConvert(00010110,2,10) -> 22 (10進数)
00001000(桁上がり)
桁上がりを除いたa+bの結果は「XOR演算」と同じ
1 and 1 -> 0
0 and 1 -> 1
1 and 0 -> 1
0 and 0 -> 0
10 bitXOr 12 (10進数)-> 110 (2進数)
format("%08s",baseConvert((10 bitXOr 12),10,2)) -> 00000110
桁上がりの箇所は「AND演算」と同じ
1 and 1 -> 1
0 and 1 -> 0
1 and 0 -> 0
0 and 0 -> 0
10 bitAnd 12 -> 8(10進数)-> 1000 (2進数)
format("%08s",baseConvert((10 bitAnd 12),10,2)) -> 00001000