functioning of bitwise and - c++

This question was asked in an interview, can someone tell what does the following code do? It gives output 15 for 150, 3 for 160, 15 for 15. What mathematical operation is it performing on 'n'.
int foo(int n)
{
int t,count=0;
t=n;
while(n)
{
count=count+1;
n=(n-1)&t;
}
return count;
}

It seems to calculate the number max(n**2-1, 0), where n is the number of 1 bits in a number's binary representation:
0 0 0b0
1 1 0b1
2 1 0b10
3 3 0b11
4 1 0b100
5 3 0b101
6 3 0b110
7 7 0b111
8 1 0b1000
9 3 0b1001
10 3 0b1010
11 7 0b1011
12 3 0b1100
13 7 0b1101
14 7 0b1110
15 15 0b1111
16 1 0b10000
17 3 0b10001
18 3 0b10010
19 7 0b10011
20 3 0b10100
21 7 0b10101
22 7 0b10110
23 15 0b10111
24 3 0b11000
25 7 0b11001
26 7 0b11010
27 15 0b11011
28 7 0b11100
29 15 0b11101
30 15 0b11110
31 31 0b11111
32 1 0b100000
33 3 0b100001
34 3 0b100010
35 7 0b100011
36 3 0b100100
37 7 0b100101
38 7 0b100110
39 15 0b100111
40 3 0b101000
41 7 0b101001
42 7 0b101010
43 15 0b101011
44 7 0b101100
45 15 0b101101
46 15 0b101110
47 31 0b101111
48 3 0b110000
49 7 0b110001
50 7 0b110010
51 15 0b110011
52 7 0b110100
53 15 0b110101
54 15 0b110110
55 31 0b110111
56 7 0b111000
57 15 0b111001
58 15 0b111010
59 31 0b111011
60 15 0b111100
61 31 0b111101
62 31 0b111110
63 63 0b111111
64 1 0b1000000
65 3 0b1000001
66 3 0b1000010
67 7 0b1000011
68 3 0b1000100
69 7 0b1000101
70 7 0b1000110
71 15 0b1000111
72 3 0b1001000
73 7 0b1001001
74 7 0b1001010
75 15 0b1001011
76 7 0b1001100
77 15 0b1001101
78 15 0b1001110
79 31 0b1001111
80 3 0b1010000
81 7 0b1010001
82 7 0b1010010
83 15 0b1010011
84 7 0b1010100
85 15 0b1010101
86 15 0b1010110
87 31 0b1010111
88 7 0b1011000
89 15 0b1011001
90 15 0b1011010
91 31 0b1011011
92 15 0b1011100
93 31 0b1011101
94 31 0b1011110
95 63 0b1011111
96 3 0b1100000
97 7 0b1100001
98 7 0b1100010
99 15 0b1100011

It is easier to find out the "mathematical operation", when function is changed to recursive:
int foo(int n, int t)
{
if( n )
return foo( (n-1) & t ) + 1
else
return 0;
}
So formula is:
F(0,t) = 0
F(n,t) = F( (n-1) & t, t ) + 1
foo(n) = F(n,n)
I don't have any idea, is that wellknown formula for counting something, or not.
You may find answer from math.stackexchange.com

That is a method known as Brian Kernighan's way to count set bits :
unsigned int v; // count the number of bits set in v
unsigned int c; // c accumulates the total bits set in v
for (c = 0; v; c++)
{
v &= v - 1; // clear the least significant bit set
}
Brian Kernighan's method goes through as many iterations as there are set bits. So if we have a 32-bit word with only the high bit set, then it will only go once through the loop.
Published in 1988, the C Programming Language 2nd Ed. (by Brian W. Kernighan and Dennis M. Ritchie) mentions this in exercise 2-9. On April 19, 2006 Don Knuth pointed out to me that this method "was first published by Peter Wegner in CACM 3 (1960), 322. (Also discovered independently by Derrick Lehmer and published in 1964 in a book edited by Beckenbach.)"

Related

VLOOKUL OR OFFSET

I have two sheets in an Excel workbook. I need the formula which creates tables by using vlookup.
I have 10 columns and 10 rows like this
1 2 3 4 5 6 7 8 9 10
2
3
4
5
6
7
8
9
10
I have tried to use Vlookup with sum but not get the actual results.
The expected result should be like this
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 42 48 56 64 72 80
9 18 27 36 49 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100

SSE - mismatch between _mm_extract_ps and direct access

The following piece of code:
__m128 var1;
float *a = (float*)malloc(50*sizeof(float));
float *ptr = a;
//Initialise a with some values
for(int i = 0; i < 50; i++)
*(a+i) = i;
//print those values
for(int i = 0; i < 50; i+=4,ptr+=4)
{
var1 = _mm_loadu_ps(ptr);
cout<<(*ptr)<<" "<<var1[0]<<" "<<_mm_extract_ps(var1, 0)<<endl;
cout<<(*ptr+1)<<" "<<var1[1]<<" "<<_mm_extract_ps(var1, 1)<<endl;
cout<<(*ptr+2)<<" "<<var1[2]<<" "<<_mm_extract_ps(var1, 2)<<endl;
cout<<(*ptr+3)<<" "<<var1[3]<<" "<<_mm_extract_ps(var1, 3)<<endl;
}
returns this output:
0 0 0
1 1 1065353216
2 2 1073741824
3 3 1077936128
4 4 1082130432
5 5 1084227584
6 6 1086324736
7 7 1088421888
8 8 1090519040
9 9 1091567616
10 10 1092616192
11 11 1093664768
12 12 1094713344
13 13 1095761920
14 14 1096810496
15 15 1097859072
16 16 1098907648
17 17 1099431936
18 18 1099956224
19 19 1100480512
20 20 1101004800
21 21 1101529088
22 22 1102053376
23 23 1102577664
24 24 1103101952
25 25 1103626240
26 26 1104150528
27 27 1104674816
28 28 1105199104
29 29 1105723392
30 30 1106247680
31 31 1106771968
32 32 1107296256
33 33 1107558400
34 34 1107820544
35 35 1108082688
36 36 1108344832
37 37 1108606976
38 38 1108869120
39 39 1109131264
40 40 1109393408
41 41 1109655552
42 42 1109917696
43 43 1110179840
44 44 1110441984
45 45 1110704128
46 46 1110966272
47 47 1111228416
48 48 1111490560
49 49 1111752704
1.45875e-42 1.45875e-42 1041
0 0 0
My questions is: Isn't _mm_extract_ps the right way of accessing the contents of an __m128 variable? Why does it print values that dont match the actual value, whereas var[0] prints the correct values. As far as I know, accessing the fields of an __m128 variable using var[0] is incorrect and may lead to problems. What exactly is the right approach, at times when I need to debug my code.
Type of a is pointer to float, when you write float == 1.0f, into memory, its representation in hex is 0x3F800000, decimal value is 1 065 353 216, so printed value is valid, _mm_extract_ps returns int, and cout prints it. Hex representation of 2.0f is 0x40000000, in decimal 1 073 741 824. You printed hex representation of float using decimal value.

Printing a table using nested while loops only

I'm trying to print the following table in C++:
1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 2 4 6 8 10 12 14 16 18 20
3 3 6 9 12 15 18 21 24 27 30
4 4 8 12 16 20 24 28 32 36 40
5 5 10 15 20 25 30 35 40 45 50
6 6 12 18 24 30 36 42 48 54 60
7 7 14 21 28 35 42 49 56 63 70
8 8 16 24 32 40 48 56 64 72 80
9 9 18 27 36 45 54 63 72 81 90
10 10 20 30 40 50 60 70 80 90 100
Using nested while loops only.
I have two main problems here:
I don't understand how to do it without using a non-nested while at the beginning (to print the first line), or an if statement for the first line.
Using setw I have problems trying to align the numbers with two digits.
Here is what I tried
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int k=0;
while(k<=10)
{cout << k << setw(5);
k++;
};
cout << "\n";
int i=1;
while(i<=10){
cout << i << setw(5);
int j=1;
while(j<=10){
cout<< i*j << setw(5);
j++;
}
cout << "\n";
i++;
}
return 0;
}
But, as said, I used a non-nested while at the beginning, and also the output is:
0 1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 2 4 6 8 10 12 14 16 18 20
3 3 6 9 12 15 18 21 24 27 30
4 4 8 12 16 20 24 28 32 36 40
5 5 10 15 20 25 30 35 40 45 50
6 6 12 18 24 30 36 42 48 54 60
7 7 14 21 28 35 42 49 56 63 70
8 8 16 24 32 40 48 56 64 72 80
9 9 18 27 36 45 54 63 72 81 90
10 10 20 30 40 50 60 70 80 90 100
Where the two-digits numbers are not aligned in the proper way. On the other hand I cannot think a way to modify the loop in order to increase the space only for two digits number, without using an if statement.
So am I missing something or it is not possible to print the table above without using if or non-nested while?
I think you want to hide first 0 value. To hide it, I have used some bit manipulations. There is no if statement and all of the printing in nested while.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i = 0, k = 0;
while (i < 10){
int j = 0;
while (j <= 10){
cout << left << setw(5);
(i || j || k) && cout << j + i * j + !j * (i + 1);
!(i || j || k) && cout << "";
j++;
}
cout << "\n";
i += k++ > 0;
}
return 0;
}
The output is
1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 2 4 6 8 10 12 14 16 18 20
3 3 6 9 12 15 18 21 24 27 30
4 4 8 12 16 20 24 28 32 36 40
5 5 10 15 20 25 30 35 40 45 50
6 6 12 18 24 30 36 42 48 54 60
7 7 14 21 28 35 42 49 56 63 70
8 8 16 24 32 40 48 56 64 72 80
9 9 18 27 36 45 54 63 72 81 90
10 10 20 30 40 50 60 70 80 90 100
Explanation:
I have used j for seed and i * j for new value for (j + 1)-th column.
It works perfectly for from first row second column to last row last column.
For first column, j is always 0. To make it work, I used !j * (i + 1) which prints 0 to 10 for first column.
To hide 0 for first row and first column, all of i, j and k are 0. For all other cells, at least one of them has value other than 0.
If still unclear, I'll explain with example.
Things to consider:
Outputting tabs instead of trying to set the width is much easier,
though you'll have less control over how your output looks (it will
depend in the tab stop width of your console). But your standard tab stops
will handle your range of values.
your problem statement doesn't say how many while loops you can use
You can use a while loop to emulate an if statement like this:
while(j == 0){
cout << i << '\t';
break;
}
I'll leave the rest as an exercise for you.

cost of cutting a plank

I know that this question has been asked but i cant understand the problem in my code. I know that we have to use cost in desending order for minimum cost and i did same but still gives wrong output.
A board composed of m×n wooden squares and asks him to find the minimum cost of breaking the board back down into individual 1×1 pieces. To break the board down, Bob must make cuts along its horizontal and vertical lines.
To reduce the board to squares, xn−1 vertical cuts must be made at locations x1,x2,…,xn−2,xn−1 and ym−1 horizontal cuts must be made at locations y1,y2,…,ym−2,ym−1. Each cut along some xi (or yj) has a cost, cxi (or cyj). If a cut of cost c passes through n already-cut segments, the total cost of the cut is n×c.
The cost of cutting the whole board down into 1×1 squares is the sum of the cost of each successive cut. Recall that the cost of a cut is multiplied by the number of already-cut segments it crosses through, so each cut is increasingly expensive.
Input Format
The first line contains a single integer, T, denoting the number of test cases. The subsequent 3T lines describe each test case in 3 lines.
For each test case, the first line has two positive space-separated integers, m and n, detailing the respective height (y) and width (x) of the board.
The second line has m−1 space-separated integers listing the cost, cyj, of cutting a segment of the board at each respective location from y1,y2,…,ym−2,ym−1.
The third line has n−1 space-separated integers listing the cost, cxi, of cutting a segment of the board at each respective location from x1,x2,…,xn−2,xn−1.
Note: If we were to superimpose the m×n board on a 2D graph, x0, xn, y0, and yn would all be edges of the board and thus not valid cut lines.
Constraints
1≤T≤20
2≤m,n≤1000000
,0≤cxi,cyj≤1000000000
Output Format
For each of the T test cases, find the minimum cost (MinimumCost) of cutting the board into 1×1 squares and print the value of MinimumCost % (1000000000+7).
#include <iostream>
#include <limits>
using namespace std;
int main() {
int t,ch=0;
long int pos,m,n,h=1,l=1;
long long int cost=0,*x,*y,temp;
cin>>t;
while(t>0)
{cin>>m>>n;
cost=0;
x = new long long int[n-1];
y = new long long int[m-1];
for (long i=0;i<m-1;i++)
cin>>y[i];
for(long i=0;i<n-1;i++)
cin>>x[i];
h=1;
l=1;
while((h!=m)|(l!=n))
{ch=0;
temp=0;
for (long i=0;i<m-1;i++)
if (temp<y[i])
{temp=y[i];
pos=i;
}
for(long i=0;i<n-1;i++)
if (temp<x[i])
{temp=x[i];
pos=i;
ch=1;
}
cost=cost+temp*(ch==0?l:h);
if (ch==0)
{y[pos]=-1;
h++;}
else
{x[pos]=-1;
l++;
}
}
cout<<cost%1000000007;
t--;
}
return 0;
}
Test case that gives wrong output:
Input:
5
52 30
2 30 79 47 4 56 47 67 25 30 75 58 47 54 66 61 6 64 28 41 75 36 1 92 42 61 35 56 12 86 84 14 68 63 13 72 19 60 39 96 43 14 55 42 21 73 3 27 37 84 68
64 72 21 56 14 35 44 71 47 82 7 14 50 71 79 23 42 92 14 39 35 81 46 29 2 19 84 81 57
23 43
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
60 76
7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
58 40
71 58 61 51 33 3 43 48 94 30 29 40 59 83 12 43 64 69 64 65 42 57 40 72 64 98 98 47 56 6 85 79 65 46 30 98 49 25 98 96 7 27 88 66 10 0 62 26 69 78 92 64 87 84 88 51 35
87 50 91 45 35 22 62 81 53 61 83 30 59 31 38 39 19 56 1 20 70 28 41 48 72 57 35 56 46 39 91 85 41 34 30 77 57 93 10
47 94
6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17
Output
51028
1912
33638
91124
27525
This line could be a contributing factor:
while((h!=m)|(l!=n))
The | operator is a binary arithmetic operator.
Maybe you were looking for ||, which is the logical OR operator:
while((h!=m) || (l!=n))

Access values from Matrix in OpenCV

For example, I have a matrix M of size 10x10 and I have a column matrix ind of length 5
How can I assign A(ind,:) to a new matrix B in C++ with OpenCV?
Below is how I do in Matlab:
A = [ 41 8 33 36 22 14 38 43 18 4
46 49 2 2 20 34 13 13 42 3
7 48 43 14 39 33 26 41 30 27
46 25 47 3 40 9 35 13 28 39
32 41 34 5 10 6 45 47 46 47
5 8 38 42 25 25 48 18 15 7
14 22 38 35 23 48 28 10 38 29
28 46 20 16 33 18 7 13 38 24
48 40 33 48 36 30 8 31 20 1
49 48 9 2 38 12 13 24 29 17]
ind = [2; 8; 4; 6; 2]
B = A(ind, :);
B = [ 46 49 2 2 20 34 13 13 42 3
28 46 20 16 33 18 7 13 38 24
46 25 47 3 40 9 35 13 28 39
5 8 38 42 25 25 48 18 15 7
46 49 2 2 20 34 13 13 42 3]
Can anyone tell me how to do this in C++ with OpenCV without using for loop
There is no direct way to extract a random ordering of rows/columns without iterating in some way. The simplest method is to extract rows and push them into the target matrix one by one. Given you have your matrix A declared and its data set:
cv::Mat B;
B.push_back(A(cv::Range(2,3),cv::Range::all()));
B.push_back(A(cv::Range(8,9),cv::Range::all()));
B.push_back(A(cv::Range(4,5),cv::Range::all()));
B.push_back(A(cv::Range(6,7),cv::Range::all()));
B.push_back(A(cv::Range(2,3),cv::Range::all()));
should do what you want. This uses the overloaded operator()(cv::rowRange, cv::colRange) to extract the selected rows.
I don't think this is possible without using for loop but the fastest way of doing this is by using memcpy. You can see the complete code here