Checking equality/inequality of multiple variables [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Let's say we have four variables: int a, b, c, d;. I need to check if excatly two of them are equal.
So for example: 1 1 9 5 is true, but 3 9 8 4 and 3 3 3 1 are false.
Of course writing an if statement for this would take a lot of time, won't be easily readable and it would be easy to make a mistake writing it.
What is the best way to write such statement?

There are several ways to do this.
One involves explicitly checking all the conditions. Since you have 4 variables, you only 6 conditions to check. These can easily be counted.
int n = (a == b) + (a == c) + (a == d) + (b == c) + (b == d) + (c == d);
Then check if n is 1. This works because a boolean value will be converted into an int (value 1 for true, 0 for false).
Another possibility is to store them all in a container (like a vector), sort it, then count the number of adjacent identical values.

Related

use of: index & 0x01 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to know about what is the use of that (index & 0x01) in the code?
if(((arr[index] >= 0) && (!(index & 0x01)))
|| ((arr[index] < 0) && (index & 0x01)))
{
outofplace = index;
}
A number is odd if and only if its last digit is odd, regardless of the base. So if we want to know the number's oddity, it's enough to check if the last bit is set.
index & 0x01
will be 1 if and only if index is odd.
If we have to deduce a general rule, we can say that for any non-negative number x,
x % y == (x & (y - 1))
provided that y is a positive power of 2.
This is a common hack in competitive coding. It is used because the competitive programmers think that bit-wise AND works faster than modulo.
In modern compilers, there is no performance difference at all. Read this thread.
There is no special reason in writing it as 0x01 instead of 1. Both compile to give the same assembly! Almost everyone (who uses this hack),= uses 1, because we have to type 3 characters extra in 0x01. :P
Here in this case, index & 0x1 is equivalent to index % 2 which is simply a condition to check if the number is odd. (Array indexes in C++ are always positive, unless you are going out of bound.)
As the other answers pointed out, while this is a well known pattern (see also this Q&A about that mask), it can be considered a premature optimization.
I'd like to suggest the following alternative to the posted code, which I find more readable. Your mileage may vary.
// Give a meaningful name.
constexpr auto is_odd = [] (auto x) -> bool {
return x % 2;
}
// Use it to simplify the condition.
if ( (arr[index] < 0) == is_odd(index) ) {
// Do something
}

How is it possible to iterate over all subsets of a set represented by bits? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I was going though this article and it explains
It is also possible to iterate over all the subsets of a particular subset (represented by a bit pattern), provided that you don’t mind visiting them in reverse order (if this is problematic, put them in a list as they’re generated, then walk the list backwards). The trick is similar to that for finding the lowest bit in a number. If we subtract 1 from a subset, then the lowest set element is cleared, and every lower element is set. However, we only want to set those lower elements that are in the superset. So the iteration step is just i = (i - 1) & superset.
I'm not able to understand it despite re-reading several times. Could someone explain with some example?
If we have some set represented as a bitmask, for example if we have the universe:
U = { A, B, C, D, E, F, G }
Then the consonants S = { B, C, D, F, G } could be represented as 0b1101110 (read from the right, the least significant bit corresponds to A), and we can iterate through subsets of this set with:
i = (i - 1) & S
Because subtracting 1 will borrow through any trailing zeroes and unset the lowest set bit, then & S clears up any bits that became set this way but are not in S. For example:
i0 = 0b1101110 (the whole S)
i1 = i0 - 1 & S = 0b1101110 - 1 & S = 0b1101101 & S = 0b1101100
So the next subset is { C, D, F, G }, dropping B for now. Then the next is
i1 = 0b1101100
i2 = i1 - 1 & S = 0b1101100 - 1 & S = 0b1101011 & S = 0b1101010
Which represents { B, D, F, G }.
By the way it can be done forwards without storing the whole thing in a list:
i = ((i | ~S) + 1) & S
Here we need an extra | ~S to set the "in between"-bits to make the + 1 carry through them, otherwise it's the same idea.

Recursion Help Please [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to figure out the following problem for an upcoming test. I have searched everywhere, and I understand the basics of recursion. What I don't understand for this particular question is the value of int n and int k respectively. I have the answer to this question as it is a practice question, but I have no idea how the answer was found.
// Precondition: n and k are non-negative integers
int f(int n, int k) {
if (k * n == 0)
return 1
else
return f(n - 1, k - 1) + f(n - 1, k)
}
What value is returned by the call f(4, 2)?
Just look at how it's called.
f(4,2) goes into 2nd block, calls f(3,1)+f(3,2)
f(3,1) calls f(2,0)+f(2,1) = 1+f(1,0)+f(1,1)=1+1+f(0,0)+f(0,1)=1+1+1+1=4
f(3,2) calls f(2,1)+f(2,2)= f(1,0)+f(1,1)+f(1,1)+f(1,2) and so on.
You should be able to work it out from here.
I am not sure what the problem is since
f(4,2)=f(3,1) + f(3,2)
=(f(2,0)+f(2,1) )+ (f(2,1) +f(2,2))
=(1 +(f(1,0)+f(1,1))+((f(1,0)+f(1,1))+(f(1,1)+f(1,2))
=(1 + 1 +(1+1)) +( 1 +(1+1) + (1+1) +1 + 1 ))
=11

Fast input method of number in c/c++? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
inline int input()
{
int c;
int n = 0;
while ((c = getchar_unlocked()) >= '0' && c <= '9')
{
// n = 10 * n + (c - '0');
n = (n << 3) + ( n << 1 ) + c - '0';
}
return n;
}
Can someone explain how this way of inputting the number is working and how it is the fast way to input a number?
Compilers are generally very stupid, and have no understanding of the logic you're trying to implement. Moreover, they're often written by less-than-competent people who don't understand much of modern hardware.
The author of the code has realized this, and cleverly analyzed that 10 is the same as 8 + 2, and that 8 and 2 are both powers of two. For the flourish, he proceeded to turn the mathematics of exponentials into native, bitwise hardware instructions. This combination of mathematics and deep understanding of the hardware leads him to factor 10 * x as 8 * x + 2 * x and express the result in terms of instructions that are far more optimal than the naive "stupid multiplication" that would otherwise have taken place. Naturally, such optimizations are far beyond the reach of any kind of technology and cannot possibly be performed automatically.
The result is a vastly improved method of multiplying a number by ten.
Patent pending.
n << 3 equals n * 8
n << 1 equals n * 2
i.e. (n << 3) + ( n << 1 ) equals 10 * n
bitwise shift is faster than multiplication, though I'm not sure the whole thing should be faster.

How to round to the nearest fourth [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm looking for a way to round a number to nearest number that can be divided by 4 without remainder
num = std::round(num / 4.0) * 4.0;
Here is some pseudo code. Probably not the most efficient way, but...
if num mod 4 == 0 then you are good
if num mod 4 == 1 then subtract 1
if num mod 4 == 2 then you decide (subtract/add 2)
if num mod 4 == 3 then add 1
Use the following MACRO:
#define ALIGN4(len) (((len) + 3) & ~3) // round up to 4 items