# LUT5

- Posts: 7855
- Karma: 194
- Thank you received: 883

The truth table seems to only show the "and" part

```
Bits
4 3 2 1 0 Weight
0 0 0 0 0 0x1
0 0 0 0 1 0x2
0 0 0 1 0 0x4
0 0 0 1 1 0x8
0 0 1 0 0 0x10
```

I tried making sense of the hex to binary conversion but that doesn't make sense either.

```
binary conversion from hex
0 0 0 0 1 0x1
0 0 0 1 0 0x2
0 0 1 0 0 0x4
```

So then I went to look at the code in the comp and that didn't make much sense either.

```
FUNCTION(_) {
int shift = 0;
if(in_0) shift += 1;
if(in_1) shift += 2;
if(in_2) shift += 4;
if(in_3) shift += 8;
if(in_4) shift += 16;
out = (function & (1<<shift)) != 0;
}
```

The man page hints that the lut5 component is magical and has three types of functions 'and', 'or', and 'xor' but the truth table only shows the 'and' I think as the or function uses 0xfffffffe and that number is not in the truth table. Some experimentation shows that you can combine and and xor in some ways so that is confusing too.

I know Peter has been trying to explain this to me in another thread but I didn't want to hijack that thread any more. I suspect that bit shifting plays a part in this with the pattern between the truth table and a straight binary conversion of the hex number.the man page wrote: A 5-input and function is TRUE only when all the inputs are true, so the correct value for function is 0x80000000.

A 5-input or function is TRUE whenever any of the inputs are true, so the correct value for function is 0xfffffffe.

A 2-input xor function is TRUE whenever exactly one of the inputs is true, so the correct value for function is 0x6. Only in-0 and in-1 should be connected to signals, because if any other bit is TRUE then the output will be FALSE.

Thanks

John

Please Log in or Create an account to join the conversation.

en.wikipedia.org/wiki/Bitwise_operation

Please Log in or Create an account to join the conversation.

- Posts: 7855
- Karma: 194
- Thank you received: 883

```
0011 (decimal 3)
AND 0010 (decimal 2)
= 0010 (decimal 2)
```

John

Please Log in or Create an account to join the conversation.

logical functions of 5 inputs so AND,OR,NAND,NOR,XOR

and every other combinatorial function is possible.

I think its easier to not look at it as a logical function generator

but rather as a lookup table. When looked at this way it becomes

simple to generate any logical function of 5 inputs.

Heres how:

write all the input states down (32 for all 5 inputs 16 for 4 inputs , 8 for 3 inputs. 4 for 2 inputs ) with the weight and the desired output state:

Inputs Weight Output

0 0 0 0 0 1 0

0 0 0 0 1 2 1

0 0 0 1 0 4 1

0 0 0 1 1 8 0

When done, add the weights of all the rows with a '1' output together and voila you have the function value for your desired logic. (6 in this case or 2 input XOR)

This could be done with a spreadsheet/python widget fairly easily

Please Log in or Create an account to join the conversation.

- Posts: 7855
- Karma: 194
- Thank you received: 883

mhaberler wrote: see here: wiki.linuxcnc.org/cgi-bin/wiki.pl?Lut5

-m

The program is flawed, it only seems to work for all 5 inputs. For example if I put :

```
./lut5.py 'i0 & i1'
# expression = i0 & i1
...
# setp lut5.N.function 0x88888888
```

but the real answer is 0x8.

If I put

```
./lut5.py 'i0 & i1 & i2 & i3 & i4'
# expression = i0 & i1 & i2 & i3 & i4
...
# setp lut5.N.function 0x80000000
```

which is correct.

Darn it!

John

Please Log in or Create an account to join the conversation.

BigJohnT wrote:

mhaberler wrote: see here: wiki.linuxcnc.org/cgi-bin/wiki.pl?Lut5

-m

The program is flawed, it only seems to work for all 5 inputs. For example if I put :

./lut5.py 'i0 & i1' # expression = i0 & i1 ... # setp lut5.N.function 0x88888888

but the real answer is 0x8.

No it isnt, go try in HAL

your expression implies 'ignore i2,i3,i4' and the right thing happens with 0x88888888 - the output reacts only to i0 and i1

with 0x8000000 all of i0-i4 must be true which is not reflected in your expression

-m

If I put

./lut5.py 'i0 & i1 & i2 & i3 & i4' # expression = i0 & i1 & i2 & i3 & i4 ... # setp lut5.N.function 0x80000000

which is correct.

Darn it!

John

Please Log in or Create an account to join the conversation.

- Posts: 7855
- Karma: 194
- Thank you received: 883

- Posts: 7855
- Karma: 194
- Thank you received: 883

PCW wrote: Note that LUT5 will generate any of the 4,294,967,296

logical functions of 5 inputs so AND,OR,NAND,NOR,XOR

and every other combinatorial function is possible.

I think its easier to not look at it as a logical function generator

but rather as a lookup table. When looked at this way it becomes

simple to generate any logical function of 5 inputs.

Heres how:

write all the input states down (32 for all 5 inputs 16 for 4 inputs , 8 for 3 inputs. 4 for 2 inputs ) with the weight and the desired output state:

Inputs Weight Output

0 0 0 0 0 1 0

0 0 0 0 1 2 1

0 0 0 1 0 4 1

0 0 0 1 1 8 0

When done, add the weights of all the rows with a '1' output together and voilĂ you have the function value for your desired logic. (6 in this case or 2 input XOR)

This could be done with a spreadsheet/python widget fairly easily

Now I finally understand.... for a 5 input or function it is all of them but one! I finally can make this work in my head... now to try and put this in layman's terms for the docs.

```
./lut5.py 'i0 | i1 | i2 | i3 | i4'
# expression = i0 | i1 | i2 | i3 | i4
#in: i4 i3 i2 i1 i0 out weight
# 0: 0 0 0 0 0 0
# 1: 0 0 0 0 1 1 0x2
# 2: 0 0 0 1 0 1 0x4
# 3: 0 0 0 1 1 1 0x8
# 4: 0 0 1 0 0 1 0x10
# 5: 0 0 1 0 1 1 0x20
# 6: 0 0 1 1 0 1 0x40
# 7: 0 0 1 1 1 1 0x80
# 8: 0 1 0 0 0 1 0x100
# 9: 0 1 0 0 1 1 0x200
#10: 0 1 0 1 0 1 0x400
#11: 0 1 0 1 1 1 0x800
#12: 0 1 1 0 0 1 0x1000
#13: 0 1 1 0 1 1 0x2000
#14: 0 1 1 1 0 1 0x4000
#15: 0 1 1 1 1 1 0x8000
#16: 1 0 0 0 0 1 0x10000
#17: 1 0 0 0 1 1 0x20000
#18: 1 0 0 1 0 1 0x40000
#19: 1 0 0 1 1 1 0x80000
#20: 1 0 1 0 0 1 0x100000
#21: 1 0 1 0 1 1 0x200000
#22: 1 0 1 1 0 1 0x400000
#23: 1 0 1 1 1 1 0x800000
#24: 1 1 0 0 0 1 0x1000000
#25: 1 1 0 0 1 1 0x2000000
#26: 1 1 0 1 0 1 0x4000000
#27: 1 1 0 1 1 1 0x8000000
#28: 1 1 1 0 0 1 0x10000000
#29: 1 1 1 0 1 1 0x20000000
#30: 1 1 1 1 0 1 0x40000000
#31: 1 1 1 1 1 1 0x80000000
# setp lut5.N.function 0xfffffffe
```

Thanks guys for taking the time to beat this in between my ears.

John

Please Log in or Create an account to join the conversation.