# Lazarus

## Miscellaneous => Other => Topic started by: Peter H on April 08, 2021, 05:08:06 am

Title: Why do we use binary logic in programming languages?
Post by: Peter H on April 08, 2021, 05:08:06 am
Because a Boolean is a byte, we could instead use quad Logic this has four values:

true,false,undetermined, dont care.

This is fully compatible to binary logic, but practically problems can be expressed much more compact and clear.
Title: Re: Why do we use binary logic in programming languages?
Post by: Martin_fr on April 08, 2021, 05:23:01 am
Feel free to create an enum, and overload operators as needed and possible....

Also "Boolean is a byte" is mislead, if not wrong. Boolean can be stored in a byte. But when bitpacked (array/record) it can be stored in a single bit.
There is also the possibility to store it in a word, longword, or quadword.

Boolean is a concept. Storage is something added by implementation.

As for other concepts, there are languages that bring in undefined and/or null. And not just for booleans, but for other types too.

Title: Re: Why do we use binary logic in programming languages?
Post by: avra on April 08, 2021, 09:40:27 am
This is fully compatible to binary logic, but practically problems can be expressed much more compact and clear.
Search the net for fuzzy logic. It's fun, very powerful and has wide appliance potential. You'll like it, I promise.  ;)
Title: Re: Why do we use binary logic in programming languages?
Post by: MarkMLl on April 08, 2021, 09:51:24 am
Search the net for fuzzy logic. It's fun, very powerful and has wide appliance potential. You'll like it, I promise.  ;)

I suppose that an answer to the "why" question is that we were building electrical switching circuits using relays a long time before we had electronics which could generate and compare voltage levels accurately.

There were obviously mechanical analysers/integrators of various types, but they were used to solve numerical calculations rather than for control purposes. I suppose that at a pinch we could call cam-based control systems non-binary, but by and large they didn't have a precise computerised equivalent.

/Technically/, Babbage could have built a relay-based computer in about 1850. In practice, it would not have been possible to get it to operate fast enough.

MarkMLl
Title: Re: Why do we use binary logic in programming languages?
Post by: Bart on April 08, 2021, 11:54:03 am
The classic True, False, FileNotFound (https://thedailywtf.com/articles/What_Is_Truth_0x3f_).

I have a Boolean factory which has support for True, False, Maybe (it compensates for the uncertainty principle in quantum mechanics).
I can give you the url if you need it.

Bart
Title: Re: Why do we use binary logic in programming languages?
Post by: Fred vS on April 08, 2021, 12:12:33 pm
Hello.

I propose to switch from binary to ternary.

It is much more natural than binary.

For example, to store data in a hard disk, with binary 0 = one part no magnetic of the disk and 1 = magnetic.

But with ternary, you can store much more infos with the same place, for example with magnet, 0 = no magnetic, 1 = magnet North-South, 2 = South-North.

The same with electricity, 0 = no current, 1 = negative, 2 = positive.

Fre;D

Title: Re: Why do we use binary logic in programming languages?
Post by: MarkMLl on April 08, 2021, 12:26:39 pm
Don't forget spin: you can store twice as much info if you reverse the direction of the disc's rotation.

MarkMLl
Title: Re: Why do we use binary logic in programming languages?
Post by: Fred vS on April 08, 2021, 12:38:32 pm
Don't forget spin: you can store twice as much info if you reverse the direction of the disc's rotation.

MarkMLl

Yes, of course, you may also store the info modulated, for example, 0 = 0 % magnet, 1 = 50 % magnet, 2 = 100 %, to keep simple and stay with ternary.
(But you may imagine base much bigger than ternary)

About the spin, yes but concretely, it will take some time for the disk to reverse the rotation, so ok, more data but slower access.
Title: Re: Why do we use binary logic in programming languages?
Post by: MarkMLl on April 08, 2021, 12:42:39 pm
That can be accommodated by duplicating the r/w logic using positronic circuitry, which has concomitant time-reversal. Balance your data and the rotational latency averages to zero.

MarkMLl
Title: Re: Why do we use binary logic in programming languages?
Post by: Thaddy on April 08, 2021, 12:50:36 pm
I already published ternary and fuzzy logic on this forum.
See here:

https://forum.lazarus.freepascal.org/index.php/topic,41144.msg290876.html#msg290876
https://forum.lazarus.freepascal.org/index.php/topic,41144.msg295325.html#msg295325 and the Zadeh fuzzer in the next post.

I also posted a five way logic.:
https://forum.lazarus.freepascal.org/index.php/topic,41144.msg290987.html#msg290987

I did do quite some improvements later, but these are the basics. Can put it in a library if you want and submit to the RTL maintainers.

These can all be basic on bit packed sets as well, except Zadeh's classic fuzzer.
Title: Re: Why do we use binary logic in programming languages?
Post by: Fred vS on April 08, 2021, 01:28:18 pm
I already published ternary and fuzzy logic on this forum.
See here:

https://forum.lazarus.freepascal.org/index.php/topic,41144.msg290876.html#msg290876
https://forum.lazarus.freepascal.org/index.php/topic,41144.msg295325.html#msg295325 and the Zadeh fuzzer in the next post.

I also posted a five way logic.:
https://forum.lazarus.freepascal.org/index.php/topic,41144.msg290987.html#msg290987

I did do quite some improvements later, but these are the basics. Can put it in a library if you want and submit to the RTL maintainers.

These can all be basic on bit packed sets as well, except Zadeh's classic fuzzer.

OK, perfect, so we are ready for the software conversion from binary to ternary.

It just lacks the hardware .

Imho, the first thing to do is to build a cpu and ram ternary.

Those elements use electricity to represent the binary base ( 0 = no current, 1 = current).
For ternary, what do you prefer,  0 = no current, 1 = positive, 2 = negative or do you prefer modulated, 0 = no current, 1 = 50%, 2 = 100% ?

Title: Re: Why do we use binary logic in programming languages?
Post by: Thaddy on April 08, 2021, 01:35:50 pm
I'd prefer -1,0,1 so it becomes balanced.
This has an advantage for ternary in the sense that some logic can be easier defined from mixed operators..
Therefor I used F,U,T in the lookups (which are bitpacked in later versions)
Title: Re: Why do we use binary logic in programming languages?
Post by: Fred vS on April 08, 2021, 01:43:19 pm
I'd prefer -1,0,1 so it becomes balanced.
This has an advantage for ternary

The same here.

And for the future, when our ternary system will be the default, we can easy create a multinary OS, with a modulated current using a sine (from -1 to 1).
Title: Re: Why do we use binary logic in programming languages?
Post by: Thaddy on April 08, 2021, 01:53:55 pm
with Zaheh's logic you can easily get waveform representations.
In effect it is multinary. (I used it in a couple of VST plugins)

BTW SETUN is a ternary hardware... :P
See https://en.wikipedia.org/wiki/Setun
Title: Re: Why do we use binary logic in programming languages?
Post by: Bart on April 08, 2021, 06:21:08 pm
Don't forget spin: you can store twice as much info if you reverse the direction of the disc's rotation.

Don't use the spin of the disk, it's too slow.
Use the quantum mechanical spin (now you also have halve spin's as a bonus).

Bart
Title: Re: Why do we use binary logic in programming languages?
Post by: Thaddy on April 08, 2021, 09:26:08 pm
Bart,
My five way logic can be used to model QBits (including spin)
Title: Re: Why do we use binary logic in programming languages?
Post by: dseligo on April 09, 2021, 09:21:52 am
And 'if', 'while' and 'repeat ... until' must be extended then.

Code: Pascal  [Select][+][-]
1. if (ternary_expression) then
2. begin
3.   // expression is true
4. end
5. else
6. begin
7.   // expression is false
8. end
9. unknown
10. begin
11.   // expression is unknown
12. end;

But how is while and repeat..until supposed to work with ternary expressions?
Title: Re: Why do we use binary logic in programming languages?
Post by: Thaddy on April 09, 2021, 11:19:08 am
I would write a case instead of if then.
And since a ternary operation can be compared to the ternary value and as such translates to a normal boolean there is no problem with loops that need a Boolean value to resolve.
Code: Pascal  [Select][+][-]
1. repeat
2. //do something that sets a ternary value
3. until ternaryvalue=T{ternarytrue}
Comparison is the key....that leads to a normal Boolean.
Title: Re: Why do we use binary logic in programming languages?
Post by: dseligo on April 09, 2021, 12:56:20 pm
I would write a case instead of if then.
And since a ternary operation can be compared to the ternary value and as such translates to a normal boolean there is no problem with loops that need a Boolean value to resolve.
Code: Pascal  [Select][+][-]
1. repeat
2. //do something that sets a ternary value
3. until ternaryvalue=T{ternarytrue}
Comparison is the key....that leads to a normal Boolean.
IOW, the normal Boolean is still base. Language doesn't know about ternary logic (in it's core).
As I am concerned, I hope it will stay as it is. I don't know what what be result of
Code: Pascal  [Select][+][-]
1. true or unknown
or
Code: Pascal  [Select][+][-]
1. true and unknown
.

What whould be result of
Code: Pascal  [Select][+][-]
1. not unknown
?

Also unknown, true or false?  :)
Title: Re: Why do we use binary logic in programming languages?
Post by: Thaddy on April 09, 2021, 01:12:27 pm
Also unknown, true or false?  :)
At least for the logic I used (Kleene and Priest), the theory for that is clearly defined.
https://en.wikipedia.org/wiki/Three-valued_logic#Kleene_logic. See the table:
not U equals U by definition. That means that a translation to boolean always results in true given both sides of a negation comparison.

It is firmly established mathematical theory and not computer science.The latter can be used for an implementation of the logic.

The theory is well explained in the link I gave you.
After all logically not u equals any other u and u so a comparison in trits from u and not u is always true, because it is u..which is boolean true.

Most other ternary logics do exactly or close to the same.
Try to understand the presented truth tables. I based all my code on that.
Title: Re: Why do we use binary logic in programming languages?
Post by: y.ivanov on April 10, 2021, 09:45:02 pm
Actually, because it is the simplest one.

Regards,