+ 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
19 ответов
+ 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).
+ 6
Are you familiar with bitwise operators?
https://www.sololearn.com/learn/4070/?ref=app
+ 5
https://en.m.wikipedia.org/wiki/Logic_gate
+ 5
What's really interesting is how you subtract using gates! 😉
+ 5
I meant using logic gate example with xor, and, etc
+ 5
With gates, there are only adder circuits....
+ 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!
+ 4
If digital devices are composed of logic-gates that only have have adder circuits, how can it do subtraction?
+ 3
Daniel Cooper Only integers are directly represented by binary bytes.
+ 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.
+ 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.
+ 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);
}
+ 3
Or
int subtract(int a, int b)
{
if (b == 0) return b;
return subtract(a ^ b, (1^a & b) << 1);
}
+ 2
note that this method only works for integers
+ 2
To understand this code u will need to learn bitwiseoparations
https://www.sololearn.com/learn/4070/?ref=app
+ 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;
0
Why wouldn't this work other number types?
0
Bitwise number series.
😑
0
cac