bit wise addtion in C++ - c++

I am looking to following code at following link
https://www.geeksforgeeks.org/divide-and-conquer-set-2-karatsuba-algorithm-for-fast-multiplication/
// The main function that adds two bit sequences and returns the addition
string addBitStrings( string first, string second )
{
string result; // To store the sum bits
// make the lengths same before adding
int length = makeEqualLength(first, second);
int carry = 0; // Initialize carry
// Add all bits one by one
for (int i = length-1 ; i >= 0 ; i--)
{
int firstBit = first.at(i) - '0';
int secondBit = second.at(i) - '0';
// boolean expression for sum of 3 bits
int sum = (firstBit ^ secondBit ^ carry)+'0';
result = (char)sum + result;
// boolean expression for 3-bit addition
carry = (firstBit&secondBit) | (secondBit&carry) | (firstBit&carry);
}
// if overflow, then add a leading 1
if (carry) result = '1' + result;
return result;
}
I am having difficulty in understanding following expressions
// boolean expression for sum of 3 bits
int sum = (firstBit ^ secondBit ^ carry)+'0';
and other expression
// boolean expression for 3-bit addition
carry = (firstBit&secondBit) | (secondBit&carry) | (firstBit&carry);
What is difference between two? What are they trying to achieve?
Thanks

To understand this, a table with all possible combinations may help. (For our luck, the number of combinations is very limited for bits.)
Starting with AND (&), OR (|), XOR (^):
a | b | a & b | a | b | a ^ b
---+---+-------+-------+-------
0 | 0 | 0 | 0 | 0
0 | 1 | 0 | 1 | 1
1 | 0 | 0 | 1 | 1
1 | 1 | 1 | 1 | 0
Putting it together:
a | b | carry | a + b + carry | a ^ b ^ carry | a & b | b & carry | a & carry | a & b | a & carry | b & carry
---+---+-------+---------------+---------------+-------+-----------+-----------+-------------------------------
0 | 0 | 0 | 00 | 0 | 0 | 0 | 0 | 0
0 | 0 | 1 | 01 | 1 | 0 | 0 | 0 | 0
0 | 1 | 0 | 01 | 1 | 0 | 0 | 0 | 0
0 | 1 | 1 | 10 | 0 | 0 | 1 | 0 | 1
1 | 0 | 0 | 01 | 1 | 0 | 0 | 0 | 0
1 | 0 | 1 | 10 | 0 | 0 | 0 | 1 | 1
1 | 1 | 0 | 10 | 0 | 1 | 0 | 0 | 1
1 | 1 | 1 | 11 | 1 | 1 | 1 | 1 | 1
Please, note, how the last digit of a + b resembles exactly the result of a ^ b ^ carry as well as a & b | a & carry | b & carry resembles the first digit of a + b.
The last detail is, adding '0' (ASCII code of digit 0) to the resp. result (0 or 1) translates this to the corresponding ASCII character ('0' or '1') again.

Related

Function to reverse the bits of the hex number in decimal and return hex value of the reversed bits

Given hex number 0x1234 passed as parameter to function I trying to write the function that will reverse the bit of the number and return that reversed number 0x2C48
I can explain the solution which I am trying to reach.
0x1234 this is the input hex number which will be given to the function
0x2C48 will be the output which I want to achieve.
4 (in binary)==> 0100; (rev_bits)==> 0010 ==>2
3 (in binary)==> 0011; (rev_bits)==> 1100 ==>C
2 (in binary)==> 0010; (rev_bits)==> 0100 ==>4
1 (in binary)==> 0001; (rev_bits)==> 1000 ==>8
There are many methods to do this. Here is one using bit shifting.
Let A be the original value, such as 1100.
Let B be the result value.
+---+---+---+---+ +---+---+---+---+
| 1 | 1 | 0 | 0 | | 0 | 0 | 0 | 0 |
+---+---+---+---+ +---+---+---+---+
A B
Step 1: Set the least significant bit in A to B.
+---------------------+
| |
^ v
+---+---+---+---+ +---+---+---+---+
| 1 | 1 | 0 | 0 | | 0 | 0 | 0 | 0 |
+---+---+---+---+ +---+---+---+---+
A B
Step 2: Shift A right by one bit. Shift B left by one bit.
+---+---+---+---+ +---+---+---+---+
| 0 | 1 | 1 | 0 | | 0 | 0 | 0 | 0 |
+---+---+---+---+ +---+---+---+---+
A B
Repeat 3 more times:
+---+---+---+---+ +---+---+---+---+
| 0 | 0 | 1 | 1 | | 0 | 0 | 0 | 0 |
+---+---+---+---+ +---+---+---+---+
A B
+---+---+---+---+ +---+---+---+---+
| 0 | 0 | 0 | 1 | | 0 | 0 | 0 | 1 |
+---+---+---+---+ +---+---+---+---+
A B
+---+---+---+---+ +---+---+---+---+
| 0 | 0 | 0 | 0 | | 0 | 0 | 1 | 1 |
+---+---+---+---+ +---+---+---+---+
A B
The implementation of the above algorithm is left for the OP.

Program to print all subset of length l

#include<iostream>
using namespace std;
void combinationUtil(int arr[], int n, int r, int index, int data[], int i);
void printCombination(int arr[], int n, int r)
{
int data[r];
combinationUtil(arr, n, r, 0, data, 0);
}
void combinationUtil(int arr[], int n, int r, int index, int data[], int i)
{
if (index == r)
{
for (int j = 0; j < r; j++)
cout << data[j] << " ";
cout << endl;
return;
}
if (i >= n)
return;
data[index] = arr[i];
combinationUtil(arr, n, r, index + 1, data, i + 1);
combinationUtil(arr, n, r, index, data, i + 1);
}
int main()
{
int n = 5;
int arr[n] = { 1, 2, 3, 4, 5 };
int k = 3;
printCombination(arr, n, k);
return 0;
}
This is the code to print all possible subsets of length k. I didn’t understand the part how the function returns to the part where it prints the subset 134 after printing the subset 125. Please explain.
I even drew the tree, but how does the code create that "13" series after printing 125 . I'm quite weak at recursion, please correct me if there's a mistake in my code or tree.
Recursion Tree:
Let's inspect the programme execution (you can do this easily using a debugger).
Firstly since arr, n and r don't change in the call tree, let's note them down first:
n := 5, r := 3, arr := {1, 2, 3, 4, 5}
For the variable arguments let's build a table and note the first call to combinationUtil:
Callstack | index | i | data | Cases (printing, returning or recursing)
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 0 | 0 | {., ., .} | recursing (index != r, i != n)
printCombination | | | |
main | | | |
Let's ignore main and printCombination in the future, but note that the top of the stack describes the function we've just entered with the given state of index, i and data (and arr, n, r but those won't change) - specifically we did not yet execute any of the body of the function like assigning data[index] = arr[i] or similar. Following the execution note how we're neither printing nor breaking out early since index != r and i != n meaning we'll recurse twice in this function call. Let's step to the first recursive call:
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 1 | 1 | {1, ., .} | recursing
combinationUtil | 0 | 0 | |
When we enter our first recursive call, 1 will have been written to data so at the beginning of the function we can now see this value in data. We'll continue twice:
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 2 | 2 | {1, 2, .} | recursing
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 3 | 3 | {1, 2, 3} | printing (index == r)
combinationUtil | 2 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
New case: index == r i.e. we'll loop over data, print its current content and return, one level higher we'll step into the next recursive call so let's look at the next steps:
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 2 | 3 | {1, 2, 3} | recursing
combinationUtil | 2 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 3 | 4 | {1, 2, 4} | printing
combinationUtil | 2 | 3 | |
combinationUtil | 2 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 2 | 4 | {1, 2, 4} | recursing
combinationUtil | 2 | 3 | |
combinationUtil | 2 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 3 | 5 | {1, 2, 5} | printing
combinationUtil | 2 | 4 | |
combinationUtil | 2 | 3 | |
combinationUtil | 2 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 2 | 5 | {1, 2, 5} | returning (i >= n)
combinationUtil | 2 | 4 | |
combinationUtil | 2 | 3 | |
combinationUtil | 2 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
Can you see how deep the call stack got until we printed 125? If you've been drawing the call graph along you should have something along the following now:
[] (0, 0)
/
1 (1, 1)
/
12 (2, 2)
/ \
123 12 (2, 3)
/ \
124 12 (2, 4)
/ \
125 ret (2, 5)
We're currently at ret and I've marked the arguments (index, i) from our current callstack along the root path (from [] to ret). We'll be returing in ret because i == n, we'll then return from each call up the tree until we reach 1 (index=1, i=1) where we return from its first recursion (index+1, i+1) so its next is (index, i+1) and the following next 6 steps together look like:
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 1 | 2 | {1, 2, 5} | recursing
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 2 | 3 | {1, 3, 5} | recursing
combinationUtil | 1 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 3 | 4 | {1, 3, 4} | printing
combinationUtil | 2 | 3 | |
combinationUtil | 1 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 2 | 4 | {1, 3, 4} | recursing
combinationUtil | 2 | 3 | |
combinationUtil | 1 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 3 | 5 | {1, 3, 5} | printing
combinationUtil | 2 | 4 | |
combinationUtil | 2 | 3 | |
combinationUtil | 1 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 2 | 5 | {1, 3, 5} | returning
combinationUtil | 2 | 4 | |
combinationUtil | 2 | 3 | |
combinationUtil | 1 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
And the call graph up to this point looks like:
[] (0, 0)
/
1 (1, 1)
/-------^-------\
12 1 (1, 2)
/ \ /
123 12 13 (2, 3)
/ \ / \
124 12 134 13 (2, 4)
/ \ / \
125 ret 135 ret (2, 5)
Your code is an example of DFS (Depth First Search). For every node, it first recurs on the leftmost child, then when it's at the rightmost child, this "part" of the function ends, essentially going back to the previous node and recurring on its children. Basically, children have priority over siblings, and left has priority over right.
If any part of this post is unclear, tell me and I will elaborate. Welcome to Stack Overflow :)

How to increase the verbosity level of SCIP solver log

I have the following SCIP solver log
time | node | left |LP iter|LP it/n| mem |mdpt |frac |vars |cons |cols |rows |
0.0s| 1 | 0 | 4 | - | 6k| 0 | 0 | 6 | 200 | 6 | 200 |
0.0s| 1 | 0 | 7 | - | 6k| 0 | 0 | 8 | 200 | 8 | 200 |
0.0s| 1 | 0 | 10 | - | 6k| 0 | 0 | 9 | 200 | 9 | 200 |
0.0s| 1 | 0 | 10 | - | 6k| 0 | 0 | 9 | 200 | 9 | 200 |
0.0s| 1 | 0 | 10 | - | 6k| 0 | 0 | 9 | 200 | 9 | 200 |
0.0s| 1 | 0 | 10 | - | 6k| 0 | 0 | 9 | 200 | 9 | 200 |
I want the log to be more verbose, as in display a new line at each LP iteration. So far I only came across
SCIP_CALL( SCIPsetIntParam(scip, "display/verblevel", 5));
This is increasing but not as much as I want and not where I want. Essentially I would like to have lines at LP iter 4, 5, 6, 7, 8, 9 and 10 too.
You cannot print a line of SCIP output at every LP iteration. You can set display/freq to 1, then SCIP will display a line at every node.
Additionally you can set display/lpinfo to true, then the LP solver will print additional information. I don't think any LP solver will print you a line for every LP iteration though . Do you use SCIP with SoPlex?
Edit: Looked and you can set the SoPlex frequency to 1 with the parameter "--int:displayfreq". I don't think you can set this through the SCIP api though. If you only want to solve the LP you could just do it in SoPlex or you would have to edit the lpi_spx2 source code.

Django Queryset two problems

my queryset :
Status.objects.filter(date__gte='2017-07-05', date__lt='2017-07-09', type='X').update(value=F('value') + 1)
my database :
date | value | value1 | value2 | type
2017-07-05 | 0 | 0 | 0 | X
2017-07-06 | 0 | 0 | 0 | X
2017-07-07 | 0 | 0 | 0 | X
2017-07-08 | 0 | 0 | 0 | X
2017-07-09 | 0 | 0 | 0 | X
2017-07-10 | 0 | 0 | 0 | X
I have two question, but my above queryset don't work.
1 - How update field "value" in date range ?
2 - How to replace "value" with a variable ?
update(value=F('value') + 1)
I need to dynamically select field (value1, value2, valuse3) from the database to change value.
you can path a field name with a variable using this.
somename='some_field' #value.value1,... in your case
Status.objects.filter(Q(date__gte='2017-07-05'), Q(date__lt='2017-07-09'), Q(type='X')).update(**{somename: F(somename)+1})

Manipulating Bit-wise Operations

There is this puzzle question of creating an equivalent bit-wise & with only | and ~ operators.
I've been doing brute force combinations of | and ~ using 6 (0110) and 5 (0101) trying to get 4 (0100), but I still cannot get the answer.
The maximum number of operation can be used is 8.
Can someone please give me hints?
What helps you here is De Morgan's Law, which basically says:
~(a & b) == ~a | ~b
Thus we can just negate this and get:
a & b == ~(~a | ~b) //4 operations
And looking at the truth table (and in fact, god bless the simplicity of binary logic, there are only four possible combintations of inputs to generate the appropriate outputs for) we can see that both are equivalent (last two columns):
a | b | ~a | ~b | ~a OR ~b | ~(~a OR ~b) | a AND b
--|---|----|----|----------|-------------|--------
0 | 0 | 1 | 1 | 1 | 0 | 0
1 | 0 | 0 | 1 | 1 | 0 | 0
0 | 1 | 1 | 0 | 1 | 0 | 0
1 | 1 | 0 | 0 | 0 | 1 | 1
Truth table time...
A B A&B !A !B !A|!B !(!A|!B)
0 0 0 1 1 1 0
0 1 0 1 0 1 0
1 0 0 0 1 1 0
1 1 1 0 0 0 1