Logical Operations on The ASCII Symbols 0 and 1

21 May 2023, By Yue LI

Definition of "ASCII bits"

The binary-digit symbols "0" and "1" are encoded as per ASCII as 0x30 and 0x31 respectively. Therefore we may call 0x30 and 0x31 ASCII bits.

Goal

Define the logical operations and, or, xor and not for the ASCII bits, in terms of typical assembly instructions such as the bit-wise AND, OR, XOR etc., so that the operators produce the expected ASCII bits on the input ASCII bits.

For instance, we want to define:

and, such that

0x30 and 0x30 = 0x30,

0x30 and 0x31 = 0x30,

0x31 and 0x30 = 0x30,

0x31 and 0x31 = 0x31;

or, such that

0x30 or 0x30 = 0x30,

0x30 or 0x31 = 0x31,

0x31 or 0x30 = 0x31,

0x31 or 0x31 = 0x31;

xor, such that

0x30 xor 0x30 = 0x30,

0x30 xor 0x31 = 0x31,

0x31 xor 0x30 = 0x31,

0x31 xor 0x31 = 0x30;

not, such that

not 0x30 = 0x31,

not 0x31 = 0x30.

Answer

Let x and y denote ASCII bits.

x and y ≝ x AND y

0x30 AND 0x30 = 0x30

0x30 AND 0x31 = 0x30

0x31 AND 0x30 = 0x30

0x31 AND 0x31 = 0x31

x or y ≝ x OR y

0x30 OR 0x30 = 0x30

0x30 OR 0x31 = 0x31

0x31 OR 0x30 = 0x31

0x31 OR 0x31 = 0x31

x xor y ≝ (x XOR y) OR 0x30

(0x30 XOR 0x30) OR 0x30 = 0x30

(0x30 XOR 0x31) OR 0x30 = 0x31

(0x31 XOR 0x30) OR 0x30 = 0x31

(0x31 XOR 0x31) OR 0x30 = 0x30

We can also use + in the place of OR.

not x ≝ (NOT x) − 0x9E

(NOT 0x30) − 0x9E = 0xCF − 0x9E = 0x31

(NOT 0x31) − 0x9E = 0xCE − 0x9E = 0x30

Thanks !