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....
Related
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
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 4 years ago.
Improve this question
Point in Polygon Test Code
int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
{
int i, j, c = 0;
for (i = 0, j = nvert-1; i < nvert; j = i++) {
if ( ((verty[i]>testy) != (verty[j]>testy)) &&
(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
c = !c;
}
return c;
}
What is the purpose of ! in c = !c; ?
Any non-zero number or non-null pointer is considered to have a logical value of "true", whereas a number with value 0 or a null pointer is considered to have a logical value of "false".
The ! operator is the logical NOT operator (as opposed to a bit-wise NOT operator). It inverts the logical value of its operand, producing the integer value 0 if the operand has a logical value of "true", and the value 1 if the operand has a logical value of "false".
! stands for logical NOT in C. So basically the value of c is being flipped from zero to non-zero and vice versa.
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 5 years ago.
Improve this question
I need to know how the output of the if statement is calculated true or false based on the following code:
#include <stdio.h>
enum designFlags {
BOLD = 8,
ITALICS = 9,
UNDERLINE = 4
};
int main() {
int myDesign = BOLD & ITALICS;
printf("%d", myDesign);
if(BOLD & ITALICS)
printf("Design is ITALIC");
else
printf("Design is not ITALIC");
return 0;
}
first go through any C books for operators.
if(BOLD & ITALICS )
'&' its a bitwise And operator . i.e it operates on bits
BOLD = 8 = 0000 1000
&
ITALICS = 9 = 0000 1001 (check the truth table of bitwise & )
-------------------------
= 0000 1000 =8
So if(8) means true so "Design is Italics" gets printed.
As per standard §6.8.4.1
the first substatement is executed if the expression compares unequal
to 0. In the else form, the second substatement is executed if the
expression compares equal to 0.
Here the result is 8 a nonzero so if statement is executed.
The bitwise and operator is applied between those two enum values resulting in the value of 8.
The name bitwise gives enough hint on how it works. You write down the binary and perform and operation bit by bit.
As per standard,§6.5.10
The result of the binary & operator is the bitwise AND of the
operands (that is, each bit in the result is set if and only if each
of the corresponding bits in the converted operands is set).
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 5 years ago.
Improve this question
I'm writing a bitset membership predicate which should handle all values of x:
int Contains(unsigned int A, int x)
{
return (x >= 0) && (x < 8 * sizeof A) && (((1u << x) & A) != 0);
}
Is there a more efficient implementation?
You can skip lower bound check if x is unsigned.
From N1570:
6.3.1.3 Signed and unsigned integers
When a value with integer type is converted to another integer type other than _Bool, if
the value can be represented by the new type, it is unchanged.
Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or
subtracting one more than the maximum value that can be represented in the new type
until the value is in the range of the new type. 60)
unsigned int y = x; // Wrap around if x is negative
return (y < CHAR_BIT * sizeof A) && (1u << y & A) != 0;
Not sure if this really brings any meaningful improvement though. Check your compiler output to make sure.
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.