This forum is closed to new posts due to low activity and a deluge of spam. It is kept online as a static historical record. If you want to read about or discuss esoteric programming languages, the Esolang wiki is the place to go. An archive of the forum is available.

## bitwise operations in bf (6)

### 1 Name: sh!oSOb.tdmyy : 2008-06-24 18:40 ID:9XZuvSNR

does anyone know how to perform fast bitwise operations like xor, and, or in brainfuck?

### 2 Name: Anonymous : 2008-07-15 23:23 ID:6JjVW7n6

Not sure, is it on the bf algorithms page?

nothing there.

### 4 Name: Anonymous : 2008-07-18 19:07 ID:W9xMBOG9

NOT seems to be very easy.

### 5 Name: Nortaneous : 2008-11-08 03:55 ID:csKV4fSJ

AND:
Output is 1 if both inputs are true. p1 is output, p2 and p3 are inputs:

[-] ;p1v0
> ;p2
[ ;enter loop if p2 is true
> ;p3
[ ;enter loop if p3 is also true
<<+>> ;p1v1
[-] ;empty p3
]
< ;empty p2
[-]
]
< ;p1

p1v1 only if p2v!0 AND p3v!0, else p1v0

OR:
Output is 1 if at least one input is true. p1 is output, p2 and p3 are inputs, p4 is temporary byte:

[-] ;p1v0
> ;p2
[>>+<<[-]] ;p4v/1 if p2v!0
> ;p3
[>+<[-]] ;p4v/1 if p3v!0
> ;p4
[<<<+>>>[-]] ;p1v1 if p4v!0

p1v1 only if p2v!0 OR p3v!0, else p1v0. The last line is necessary to make sure p1v1 and not p1v2 (if both inputs are true).

XOR:
Output should only be 1 if sum of inputs is 1. p1 is output, p2 and p3 are inputs, p4 is temporary byte:

[-] ;p1v0
>>>[-]<< ;p4v0 p2
[>>+<<[-]] ;p4v/1
> ;p3
[>+<[-]] ;p4v/1
>- ;p4v\1 p4v0 if XOR is true
<<<+>>> ;p1v1 p4
[<<<->>>[-]] ;p1v\1
<<< ;p1

p1v1 only if p2v!0 XOR p3v!0, else p1v0

NOT:
Output should be 1 if input is 0. p1 is output, p2 is input:

[-]+ ;p1v1
>[<->[-]]< ;p1v0 if p2v!0

p1v1 only if p2v!0

Of course, you can also combine these ports to create ports such as IN1 AND NOT(IN2), or IN1 NAND IN2.