-1

Is that possible? Can this be done using just 1 and 0 (true/false, on/off ...)?

If so, how would this code look?

If this example is too complex i am open to all other kinds of examples, but would like to have an operation included, because i have no idea how such operations get encoded (i guess they also are just an entry in a conversion chart)

The reason why i ask this, is that i want to give people a concrete example why datatypes and functions/operations are a practical abstraction (easier to read). Im writing a tutorial.

Nils Lindemann
  • 1,146
  • 1
  • 16
  • 26
  • Are you talking about a specific programming language like C or ASM? Or how the plus operation is physically implemented on the cpu? This is extremely vague. – bitmask Apr 24 '20 at 10:46
  • Physically implemented. How does it look when just using 1 and 0. – Nils Lindemann Apr 24 '20 at 10:47
  • Almost all CPUs use "only 1 and 0". And there's a ton of info out there on how this type of thing works. (e.g. https://en.wikipedia.org/wiki/Logic_gate) – Mat Apr 24 '20 at 10:49
  • Have a look also at https://en.wikipedia.org/wiki/Adder_(electronics) – bitmask Apr 24 '20 at 11:00
  • @bitmask what is the definition of `S` and `C` in the sentence `The half adder adds two single binary digits A and B. It has two outputs, sum (S) and carry (C) ` in the wikipedia lemma given by you? – Nils Lindemann Apr 24 '20 at 11:46
  • It says so in the sentence? S is the sum, C the carry. If you add two one-bit values, you cannot necessarily express the result in one bit. Read the article, it literally explains what a carry is in the **next sentence**. If you are writing a tutorial for other people you should know this, and if you don't you should not tutor other people (before you understand what you teach). – bitmask Apr 24 '20 at 11:49
  • Oh well, i have read `sum (S)` as function call. But it means 'the sum, which we call S from now on' ... ok. – Nils Lindemann Apr 24 '20 at 15:13
  • thx @PeterCordes, this link brought me to [this hello world example](http://timelessname.com/elfbin/), which is much cooler than those boring wikipedia lemmas :-p – Nils Lindemann Apr 25 '20 at 01:06

1 Answers1

1

In a 1-bit wide integer = boolean value, carry-out has nowhere to go, so addition simplifies to just XOR.


Fun fact: XOR is add-without-carry. It's part of implementing a single-bit adder out of logic gates, e.g. a "half adder" that has 2 inputs (no carry-in) and produces a sum and carry-out. (sum = a xor b, carry = a AND b). A simple 32-bit adder could be build out of a half adder and 31 "full adders". Or more adders in parallel with tricks to optimize it for lower latency than a simple ripple-carry binary adders.

Carryless multiplication is a thing in some crypo, where summing partial products is done with XOR instead of normal binary addition.

See also What is the best way to add two numbers without using the + operator? for a software use of the same idea.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • So the `1 + 1` would result in doing (binary) `1 XOR 1`, which is 0 and `1 AND 1`, which is 1 and these two bits are then joined together, resulting in 01, which is decimal 2, right? What i wonder, how are these actions written in binary notation? What are the binary digits which represent 'do a XOR' and 'do a AND'? Doesnt the computer transform the source code saying '1+1' anyhow into a bunch of 1es and 0es, before it executes these? – Nils Lindemann Apr 24 '20 at 15:13
  • 1
    @Nils: Oh, I think I completely misinterpreted the question title and what you were asking. In the machine code instructions for a real CPU, you'd just see an `add` instruction. Disassemble any executable (like `objdump -drwC -Mintel /bin/ls`) and you'll see ADD instructions, along with the machine code, for example `83 c0 01` encodes the instruction `add eax,0x1` for x86-64. as you can see from https://www.felixcloutier.com/x86/add. Software that wants to do addition just uses an opcode for a binary addition instruction without caring how it's implemented internally. – Peter Cordes Apr 24 '20 at 19:36