Is Zero less than or equal to zero? [closed] - c

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 7 years ago.
Improve this question
Assume I get a value i=0;
Then will the following variable leftsmaller be true?
while(i<=0 || arr[i]<arr[j])
leftsmaller = true;

No zero is not less then zero, i <= 0 becomes 1 because zero is less than or equal to zero.

The <= ("less than or equal") operator returns true if the left hand side is less than or equal to the right hand side. Since 0 is, of course, equal to 0, so this expression will evaluate to true, which will cause the code enter the while loop, assigning true to leftsmaller.

Of course zero isn't less than zero, but i <= 0 becomes 1 because zero is less than or equal to zero.

The variable type of i is never mentioned.
If it is unsigned i then the < 0 part of the test will fail.
If it is float i then the == 0 part of the test can fail - even when you could swear that i should be 0, since inexact values can be stored.

Related

Why does this return 42 instead of 682? [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 2 years ago.
Improve this question
Working with bits in C. I am trying to create a function that looks for a specific string of bits in a larger bit string. My thinking was to compare the two using the & operator, then shifting the string left and repeating. If the operator returns the larger number (because no bits should change), the pattern has been found. Unfortunately, that seems to be a flawed method, since what I have narrowed down to is that when this happens it in fact returns the smaller string, throwing what I thought I knew into doubt.
I suppose my guess is that the zeroes in front aren't in fact added when doing this operation, but i'm wary of rationalizing the wrong theory in my coding, so here I am. Please explain, none of the resources I have found really address what is going on here. Thanks in advance.
int main (void)
{
unsigned int x = 682; // 1010101010
unsigned int pattern = 42; // 101010
unsigned int temp;
temp = x & pattern;
printf("%i\n", temp);
return 0;
}
Because & compares the bits in bitwise order. For each pair of bits it will return 1 if both are 1 otherwise 0. The 42 in binary has an implicit set of zeros in front of it.
The comparison is
0000101010
1010101010
-----------
0000101010

if (n!=0) n=0; v/s n=0; Which is more efficient and why? [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 3 years ago.
Improve this question
While typing a program as a high level programmer, n = 0; looks more efficient and clean.
But is n = 0; really more efficient than if (n != 0) n = 0;?
when n is more likely to be 0.
when n is less likely to be 0.
when n is absolutely uncertainty.
Language: C (C90)
Compiler: Borland's Turbo C++
Minimal reproducible code
void scanf();
void main()
{
int n; // 2 bytes
n=0; // Expression 1
scanf("%d",&n); // Absolutely uncertain
if(n!=0) n=0; // Expression 2
}
Note: I have mentioned the above code only for your reference. Please don't go with it's flow.
If your not comfortable with the above language/standard/compiler, then please feel free to explain the above 3 cases in your preferred language/standard/compiler.
If n is a 2's complement integral type or an unsigned integral type, then writing n = 0 directly will certainly be no slower than the version with the condition check, and a good optimising compiler will generate the same code. Some compilers compile assignment to zero as XOR'ing a register value with itself, which is a single instruction.
If n is a floating point type, a 1s' complement integral type, or a signed magnitude integral type, then the two code snippets differ in behaviour. E.g. if n is signed negative zero for example. (Acknowledge #chqrlie.) Also if n is a pointer on a system than has multiple null pointers representations, then if (n != 0) n = 0; will not assign n, when n is one of the various null pointers. n = 0; imparts a different functionality.
"will always be more efficient" is not true. Should reading n have a low cost, writing n a high cost (Think of re-writing non-volatile memory that needs to re-write a page) and is likely n == 0, then n = 0; is slower, less efficient than if (n != 0) n = 0;.
n = 0;
will always be more efficient as there is no condition check.
https://godbolt.org/z/GEzfcD

Logical Operators and Initialization in a variable [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 7 years ago.
Improve this question
What is the concept behind expressions like this?
int x;
x=7||6;
From the C11 draft specification
6.5.14 Logical OR operator
The || operator shall yield 1 if either of its operands compare
unequal to 0; otherwise, it yields 0. The result has type int.
So in the expression x = 7||6;, the || yields 1 because at least one (in fact both) of the operands compare unequal to 0.
int x;
x=7||6; // it is true always .
The output 1, you get is due to bool is promoted to int . As you can assume -
true==1
x = 7 || 6;
x = 7 "or" 6; //or can only apply to booleans
x = true or true; //Auto cast to booleans
x = true;
x = 1; //Auto cast to int
And please do some minimal effort in your class problems....

In what circumstances would one use bit shifting operators in a for loops termination condition? [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 7 years ago.
Improve this question
I have been looking at some code that fills arrays with samples created using an IFFT (Inverse Fast Fourier Transform).
When the author iterates the array he uses an if construct that looks like this:
int idx;
for (idx = 1; idx < (tableLen >> 1); idx++) {
freqWaveRe[idx] = 1.0 / idx; // sawtooth spectrum
freqWaveRe[tableLen - idx] = -freqWaveRe[idx]; // mirror
}
Can you explain the terminating condition:
idx < (tableLen >> 1)
Why would you do something like this and what does it mean?
The bit shift operator used in this expression:
idx < (tableLen >> 1)
Terminates the for loop after iterating through the first half of the array. The right shift operator moves the value one bit to the right. Moving it one bit to the right divides it by two.
1010 in binary = 10
If we right shift it one bit we get:
0101 in binary = 5
A couple more things:
Tony D mentioned some comments made that this 'will not work well if idx is negative'. Negative numbers are represented differently. Sometimes negatives are stored with the first bit representing the sign. If you shift the sign right you will lose that information and cause a bit of a mess.
Tony D also said "it was historically an optimisation when bit-shifting opcodes executed faster than division, and optimisers couldn't be trusted"

how do I find if any bit of x equals 1 in C [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 9 years ago.
Improve this question
Using only bit-wise and logical operations, how can I detect if any bit of x equals to 1 in C? No loops, if/else, ==, or !=
Someone proposed (x & ~0) && 1, but I've been looking at it for hours and still don't know how (x & ~0) && 1 can detect if any bit of x equals to 1
!!x will do it.
This will convert all values to 1, except for 0 which stays 0. And compilers can optimize this intelligently.
0 is a number with all its bits set to 0.
~ is bitwise NOT - it changes all 0 bits to 1 bits and 1 bits to 0 bits, so ~0 gives a number with all bits set to 1.
& is bitwise AND - & of two numbers returns a number where each bit corresponds to the logical AND of the corresponding bits in each of the numbers. Since ~0 consists of all 1 bits already, x & ~0 will simply return a number where each bit corresponds to the same bit in x, thus it will return x.
See Wikipedia on bitwise operations (AND and NOT) for more information.
Finally && is a boolean AND operator which will return 1 only if both sides are non-zero. 1 is not zero, thus it simply checks if (x & ~0) = x (as we established above) is not zero, thus if any of the bits in x are 1.
You should notice that there's a lot of redundancy going on here - as already pointed out, you can just use x instead.
Is this your class assignment?
You wrote "how can I detect ...", but what do you expect for the result of your "detection"?
If the result should be an integer to be fed to if/else, then you can directly feed the original x to if/else, because C's if/else can work with any integers and when "any bit of" the input equals 1, the then clause will be executed. Only when the input is exactly 0, the else clause will be executed. But if your question is your class assignment, this will not be the answer you're looking for.
If you want either 0 or 1 and don't want anything else, use #StilesCrisis's answer. Because
int y = ! x
is equivalent to
int y;
if (0 == x)
y = 1;
else
y = 0;
and therefore
int z = !! x
(whose verbose version is
int z = ! ( ! x )
) is equivalent to
int z;
if (0 == x)
z = 0;
else
z = 1;
Finally, (x & ~0) && 1 is as pointless as x + 0.

Resources