Problems with logical operators - logical-operators

I have this code:
int i = -6, a= -06;
while (a--, a+=2)
{
a-=2;
++i;
printf("OK\n");
if(!i)
break;
}
int b= -0xe;
b+= a && a++;
printf("%d %d\n", a,b);
I can't understand why at the end b is equal to -13 and not to -25. a is equal to -11 and everything is ok here, but why b is -13?

before evaluating the statement b+= a && a++;
the actual values of a and b are b=-14 and a=-12. The compiler will evaluate this expression like this b=b+(a&&a++). So, (a&&a++) will return 1 that's why you are getting -13 as the value of b.

The a && a++ returns -12 because the compiler will evaluate this expression like this (a&&a); a=a+1;, the -12 in binary equals to 1111111111111111111111111111111111111111111111111111111111110100, The && operator And each bit with itself. The (a&&a) calculate like this
1111111111111111111111111111111111111111111111111111111111110100
&&
1111111111111111111111111111111111111111111111111111111111110100
_________________________________________________________________
1111111111111111111111111111111111111111111111111111111111110100
So (a&&a) = a and the statement b+= a && a++; evaluate as b=b+(-12); a++

Related

difference between a == 10 || 20 and a == 10 || a == 20 these two

let s = 10;
if(s == 10 || s == 20){
console.log(s)
}
if( s == 10 || 20){
console.log(s)
}
// What is the defrence between these two conditions
I tried both condition and the output were the same. So what's the difference between them. It may be the stupid question but i want to konw the difference. Thanks in advance
In the first one IF (s == 10 || s == 20) the entire IF condition is TRUE if either boolean expression s == 10 OR s == 20 is TRUE. As this is processed, once the first boolean expression is TRUE the condition is met so the s==10 triggered that and console.log(s) was called.
In the second condition ( s == 10 || 20) the entire condition is TRUE if either boolean expression s==10 OR 20 is TRUE. It succeeds for the same reason as stated above. Typically a non-zero value means TRUE so in your programming language having a value of twenty there is the same as if you had the keyword TRUE there so no syntax error was thrown, even though it really does not make any sense and is indeed a logic error in the code (assuming you meant it to work the same as the first example).
This is why code reviews are a good idea.

How to handle (x,1) base case for is_power(x,y) function, where x != 1?

Title says most of it. Here is a related thread, but I do not have enough reputation to comment.
Here is the prompt:
A number, a, is a power of b if it is divisible by b and a/b is a power of b. Write a function called is_power that takes parameters a and b and returns True if a is a power of b.
In the archived post, the most upvoted answer was to address the trivial base cases of (x,x) and (1,x). Then, determine if (a % b == 0) and then recursively call to find (a/b is a power of b).
Here is the code I've written in Julia:
function ispower(a,b)
if a == 0 && b != 0
return false
elseif a == b
return true
elseif a % b == 0 && ispower(a/b, b)
return true
else
return false
end
end
As I wrote in the comment if you multiply 1 by 1 you can only get 1, so the only number that is a power of 1 is 1 and no other number. Therefore, your function should return false for an input (x,1), whenever x is not 1.
Then you should also consider the case where b==0. Then the only number that is a power of b is 0.
So, I would write you function like this:
function ispower(a::Integer,b::Integer)
if (a==b)
return true
elseif (a==0 || b==0 || b==1)
return false
elseif (a % b) == 0
return ispower(div(a,b), b)
else
return false
end
end

How to eliminate division inside code like "a/b>c/d"?

For example, if I don't want division in "a/b==c/d",I can rewrite it as "ad==bc".
But "a/b>c/d" is not always = "ad>bc", for example:
a=20, b=5, c=9,d=3 : a/b>c/d is true and ad>bc is true
but
a=-20, b=-5, c=9,d=3 : a/b>c/d is true but ad>bc is false
I started to found the patten, it seems (I am not sure if it is right):
if b and d has same sign, a/b>c/d = ad > bc
if b and d has different sign, a/b > c/d = ad < bc
if I rewrite "a/b>c/d" as
(( (b>0 && d>0 || b<0 && d<0) && a*d>b*c) || ( ((b>0 && d<0) || b<0 && d>0) && a*d<b*c)
,it needs type more characters, also I need to type each variable more than 1 time,and this form is very hard to maintain if I want to change to
a/b>=c/d
or
a/b<c/d
Is there any method to eliminate division in a/b>c/d in simpler way?
You can rewrite it as:
a*d*b*d > b*c*b*d
This way if b and d have different signs then b*d will be a negative number and the comparison will be flipped back the correct way. Be careful about overflows however. To make the code equivalent to a/b > c/d you will also need to check if b or d are zeros.
May be you can get the signs of b and d by bitwise operation, and then rewrite "a/b>c/d" as
(((b>>31)&1) ^ ((d>>31)&1)) ^ (ad > bc)
which is equivalent to
((b>0) ^ (d>0)) ^ (ad > bc)
say b and d are 32-bits integers
When you multiply both sides of an inequality by some non-zero number v, then if v is negative you must reverse the direction of the inequality.
So in your case you are multiplying by b*d so
if (a/b > c/d) {
...
}
is equivalent to
v = b * d;
if(v==0) {
error
}
if ( ( (v>0) && (a*d > c*b) ) ||
( (v<0) && (a*d < c*b) ) ) {
...
}
If you don't care whether you're using > or >=, or care about the handling of v==0 you can rewrite this as
if ( (v>0) ^ (a*d > c*b ) ) {
...
}
There's further considerations required if a, b, c and d are floating point numbers as you need to consider the behaviour of positive and negative infinity, signed zeros and "not a number" values.

Divide&Conquer Algorithm for Checking if a list is sorted in Descending order

I need to write a function that checks if an array/list/vector(in R)/
is sorted in Descending order. The answer must be implemented using Divide&Conquer.
Here is what I tried(The Implementation is in R):
is.sort<-function(x){
n<-length(x)
if (n==1)
return(T)
mid<-floor(n/2)
x1<-is.sort(x[1:mid])
x2<-is.sort(x[(mid+1):n])
return(ifelse(x1>=x2,T,F))
}
But thats always returns T :/
Thanks for the Help
It is quite awkward to implement that using D&C. However, let's try.
A sequence of length 1 is always sorted.
If x is of length >= 2, then divide it into 2 non-empty subsequences x1 and x2. Then x is sorted if x1 is sorted, x2 is sorted, and the last element in x1 is greater than or equal to (assuming a non-increasing ordering) the first element in x2.
This may be implemented as:
is.sort<-function(x){
n<-length(x)
if (n==1)
return(TRUE)
mid<-floor(n/2)
return(x[mid] >= x[mid+1] && is.sort(x[1:mid]) && is.sort(x[(mid+1):n]))
}
BTW, R has a is.unsorted function, but it determines only if a sequence is sorted w.r.t. < or <=. For those who miss its >= equivalent here's a trivial Rcpp implementation:
Rcpp::cppFunction('
bool is_sort(NumericVector x) {
int n=x.size();
for (int i=0; i<n-1; ++i)
if (x[i] < x[i+1]) return false;
return true;
}
')

What does "&&" do?

I can't seem to find the resource I need. What does && do in a code that is comparing variables to determine if they are true? If there is a link with a list of the symbol comparisons that would be greatly appreciated.
example: Expresssion 1: r = !z && (x % 2);
In most programming languages that use &&, it's the boolean "and" operator. For example, the pseudocode if (x && y) means "if x is true and y is true."
In the example you gave, it's not clear what language you're using, but
r = !z && (x % 2);
probably means this:
r = (not z) and (x mod 2)
= (z is not true) and (x mod 2 is true)
= (z is not true) and (x mod 2 is not zero)
= (z is not true) and (x is odd)
In most programming languages, the operator && is the logical AND operator. It connects to boolean expressions and returns true only when both sides are true.
Here is an example:
int var1 = 0;
int var2 = 1;
if (var1 == 0 && var2 == 0) {
// This won't get executed.
} else if (var1 == 0 && var2 == 1) {
// This piece will, however.
}
Although var1 == 0 evaluates to true, var2 is not equals to 0. Therefore, because we are using the && operator, the program won't go inside the first block.
Another operator you will see ofter is || representing the OR. It will evaluate true if at least one of the two statements are true. In the code example from above, using the OR operator would look like this:
int var1 = 0;
int var2 = 1;
if (var1 == 0 || var2 == 0) {
// This will get executed.
}
I hope you now understand what these do and how to use them!
PS: Some languages have the same functionality, but are using other keywords. Python, e.g. has the keyword and instead of &&.
It is the logical AND operator
(&&) returns the boolean value true if both operands are true and returns false otherwise.
boolean a=true;
boolean b=true;
if(a && b){
System.out.println("Both are true"); // Both condition are satisfied
}
Output
Both are true
The exact answer to your question depends on the which language your are coding in. In R, the & operator does the AND operation pairwise over two vectors, as in:
c(T,F,T,F) & c(T,T,F,F)
#> TRUE FALSE FALSE FALSE
whereas the && operator operated only on the first element of each vector, as in:
c(T,F,T,F) && c(T,T,F,F)
#> TRUE
The OR operators (| and ||) behave similarly. Different languages will have different meanings for these operators.
In C && works like a logical and, but it only operates on bool types which are true unless they are 0.
In contrast, & is a bitwise and, which returns the bits that are the same.
Ie. 1 && 2 and 1 && 3 are true.
But 1 & 2 is false and 1 & 3 is true.
Let's imagine the situation:
a = 1
b = 2
if a = 1 && b = 2
return "a is 1 and b is 2"
if a = 1 && b = 3
return "a is 1 and b is 3"
In this situation, because a equals 1 AND b = 2, the top if block would return true and "a is 1 and b is 2" would be printed. However, in the second if block, a = 1, but b does not equal 3, so because only one statement is true, the second result would not be printed. && Is the exact same as just saying and, "if a is 1 and b is 1".

Resources