+ 13

Can somebody explain this code?

I got it online. Took it so I could ask about it. Can someone explain in depth how this works? https://code.sololearn.com/c60Qd55FDaS2/?ref=app

8th Jan 2019, 6:02 AM
Daniel Cooper
Daniel Cooper - avatar
19 Respuestas
+ 11
This is simple adder, it's how the electronic logic gates (inside IC) produces arthmetic output. From the logics, sum of two bits can be obtained by performing XOR (^) of the two bits (a ^ b). Carry bit can be calculated by AND (&) of two bits (a & b) and shift (<<) the byte to 1bit left. So we have a = 0011 (3) and b = 0111 (7). then, ------------------------------------------------------------------------------ 1st iteration: sum = 0011 ^ 0111 = 0100 a & b = 0011 & 0111 = 0011 carry = 0011 << 1 = 0110 ---------------------------- 2nd iteration: sum = 0100 ^ 0110 = 0010 a & b = 0100 & 0110 = 0100 carry = 0100 << 1 = 1000 ---------------------------- 3rd iteration: sum = 0010 ^ 1000 = 1010 a & b = 0010 & 1000 = 0000 carry = 0000 << 1 = 0000 ----------------------------- So finally answer is final value of sum which is 1010 (10).
8th Jan 2019, 7:57 AM
Calviղ
Calviղ - avatar
+ 6
Are you familiar with bitwise operators? https://www.sololearn.com/learn/4070/?ref=app
8th Jan 2019, 6:43 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 5
https://en.m.wikipedia.org/wiki/Logic_gate
9th Jan 2019, 8:48 AM
Da2
Da2 - avatar
+ 5
What's really interesting is how you subtract using gates! 😉
9th Jan 2019, 9:00 AM
Da2
Da2 - avatar
+ 5
I meant using logic gate example with xor, and, etc
9th Jan 2019, 9:16 AM
Da2
Da2 - avatar
+ 5
With gates, there are only adder circuits....
9th Jan 2019, 9:18 AM
Da2
Da2 - avatar
+ 4
This is a good example of how logic gates work. These are the building blocks of digital intehrated circuits. Understanding how they work (and, or, nand, etc. gates) are what make computer work!
9th Jan 2019, 8:46 AM
Da2
Da2 - avatar
+ 4
If digital devices are composed of logic-gates that only have have adder circuits, how can it do subtraction?
9th Jan 2019, 9:27 AM
Da2
Da2 - avatar
+ 3
Daniel Cooper Only integers are directly represented by binary bytes.
8th Jan 2019, 12:44 PM
Calviղ
Calviղ - avatar
+ 3
This article better explains floating point arithmetic operations than I could. We can see there are other steps involved, due to the way floating point values are represented in binary: https://www.doc.ic.ac.uk/~eedwards/compsys/float/ Finally, note the original code is oriented to integer numbers, as all variables are of type int. Nevertheless, the steps can be applied to integers only.
8th Jan 2019, 1:09 PM
Carlos García
Carlos García - avatar
+ 3
XOR and AND are the most important logic gates in integrated circuit. For swapping between a and b using XOR: a^(a^b) = b b^(a^b) = a For investor a^1 = inverted a Other usage including parity generator, parity checker and comparator that check for 2 inputs with identical signals or not.
9th Jan 2019, 8:58 AM
Calviղ
Calviղ - avatar
+ 3
Subtraction is the opposite operation of addition, int subtract(int a, int b) { if (b == 0) return a; return subtract(a ^ b, (~a & b) << 1); }
9th Jan 2019, 9:14 AM
Calviղ
Calviղ - avatar
+ 3
Or int subtract(int a, int b) { if (b == 0) return b; return subtract(a ^ b, (1^a & b) << 1); }
9th Jan 2019, 9:16 AM
Calviղ
Calviղ - avatar
+ 2
note that this method only works for integers
8th Jan 2019, 9:54 AM
Carlos García
Carlos García - avatar
+ 2
To understand this code u will need to learn bitwiseoparations https://www.sololearn.com/learn/4070/?ref=app
10th Jan 2019, 4:24 AM
Nibi Cyankzeki
Nibi Cyankzeki - avatar
+ 1
#include<<iostream> using namespace std; int main() { int a,b,diff; cout<<"please insert a and b"; cin>>a>>b; diff=a-b; cout<<"diff"<<diff<<endl;
10th Jan 2019, 4:41 AM
Muhammed
Muhammed - avatar
0
Why wouldn't this work other number types?
8th Jan 2019, 10:03 AM
Daniel Cooper
Daniel Cooper - avatar
0
Bitwise number series. 😑
8th Jan 2019, 7:43 PM
Asim Farheen
Asim Farheen - avatar
0
cac
9th Jan 2019, 11:22 PM
Вадим Малаканов
Вадим Малаканов - avatar