What does this ampersand mean in C? [duplicate] - c

This question already has answers here:
What are bitwise operators?
(9 answers)
C language What this code mean ? if(button & 1)==1 [closed]
(4 answers)
Closed 8 years ago.
I am looking at the c code:
if((VAR_ON&3) > 1)
I am not sure what kind of variable VAR_ON is, my guess is it is a pointer, but what does the &3 at the end do to it? I apologize if this is a duplicate question, I just could not find any question regarding the ampersand AFTER a variable.

As used in the question code, the ampersand '&' is a bitwise 'and' operation.
Example (assuming that VAR_ON = '21'):
VAR_ON 21(Decimal) 00010101(Binary)
& 3(Decimal) & 00000011(Binary)
------------ ------------------
1(Decimal) 00000001(Binary)
Hence if VAR_ON is '21', the expression (VAR_ON&3) will evaluate to '1'. The 'if' condition would be false:
if((VAR_ON&3) > 1)

& in that context will perform a bit wise AND operation.
So whatever VAR_ON is will be ANDed with 3 so that only the last 2 bits of the variable will be used in the comparison.

It's a bitwise AND. That's not an ampersand "after a variable". That's a bitwise binary operator working on two operands: VAR_ON and 3.

Related

Bitwise & evaluation [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 6 years ago.
Improve this question
For the below sample code:
// Note that a, b, c and d can have value of 0 or 1 only
int isAllTrue(int a, int b, int c, int d)
{
return (a && b && c && d);
// THIS ALSO RETURNS CORRECT VALUE
// return (a & b & c & d);
}
If we know that operands can be either 0 or 1, which operation would be preferable from performance point of view: bitwise or logical? Does it really matter?
Does evaluation stop in the case of bitwise "&" when the operand is 0 ?
Use & for bit operations. Use && for logical operations. If you use a bitwise operation where you should use a logical operation or vice versa, the reader will (a) be confused, (b) check very carefully if your code has introduced a subtle bug.
Programming relies on idioms. If your code doesn't match the expected idiom, then you must have a good reason, and you should write a comment explaining why.
It doesn't matter which formulation you use, with active compiler optimization, all shown evaluations should already be completed at compile time.
As for your last question, the bitwise operators do not employ shortcut evaluation and thus always evaluate each operand.
there seems to be a lot of confusion about questions like this one ... and a lot of wrong answers. Well, here are the facts for C/C++ :
&&, || and ! are logical operators, they test for
boolean expressions - integral values (numbers) are considered true if their value is not zero, false otherwise -
no matter how many numbers you check for but 0 && 1 will be evaluated to false, for instance because of the first 0 -- it could be re-written as false && true
&, |, ~, ^, >>, << are bitwise operators, they mask/assign
bit patterns of integral values, you can do a lot of cool calculations like that - but you really need to know how numbers are represented inside of your CPU before you start doing that.
Conclusion : your question doesnt make sense - because you thought that these operators are similar .... they arent.

How to return 1 if the ASCII is uppercase [duplicate]

This question already has answers here:
Testing if an integer is an uppercase ASCII letter using bit manipulation
(4 answers)
Closed 7 years ago.
I have to return 1 if x is in between 0x41 and 0x5a (ascii for 'A' to 'Z'). Only bitwise operators are allowed (~, &, ^, |, <<, >>), plus ! and +. Library functions like isupper are not.
I'd appreciate any tips or help because I understand which ranges work, but I don't know how to manipulate it to return 1 with the given operators.
Hint 1 - Subtraction can be accomplished by adding negative numbers.
Hint 2 - Negation can be done with bitwise operators: -n == ~n+1.
Hint 3 - Right shifting a non-negative number by n is equivalent to dividing by 2n: n>>1 == n/2, n>>2 == n/4, etc.

Why is this statement with bitwise operators the same as this if statement? [duplicate]

This question already has answers here:
How is if-statement and bitwise operations same in this example?
(3 answers)
Closed 8 years ago.
I was reading this question Why is it faster to process a sorted array than an unsorted array? and the best answer provided by
Mysticial. The answer does a great job of explaining what is happening and why and also says this:
So what can be done?
If the compiler isn't able to optimize the branch into a conditional
move, you can try some hacks if you are willing to sacrifice
readability for performance.
Replace:
if (data[c] >= 128)
sum += data[c];
with:
int t = (data[c] - 128) >> 31;
sum += ~t & data[c];
This eliminates the branch and replaces it with some bitwise
operations.
What exactly is this code doing and why is it equivalent?
It first converts the comparison to subtraction with 128.
Then the sign of the result (whether subtracting went below 128) is expanded to either all zeroes or all ones, which is anded to the value being added, zeroing it out if the result of the subtraction was negative.

how does compiler work during assignment operations? [duplicate]

This question already has answers here:
How does the Comma Operator work
(9 answers)
Closed 8 years ago.
Why is a assigned the value 3? Does the compiler simply take the last value from the list?
int a;
a=(1,2,3);
printf("%d",a);
How does compiler parse this statement or how it works internally?
Comma in (1,2,3) is a comma operator. It is evaluated as
a = ( (1,2) ,3 );
Comma operator is left associative. The result/value of the expression (1,2,3) is the value of the right operand of comma operator.
As pointed out in the comments, it's because you're using the comma operator. That means the 1 and 2 are evaluated and discarded. The three is the only thing left to be assigned. Without the parenthesis it would most likely be assigned as 1.

Is the assignment expression after "sizeof" never executed? [duplicate]

This question already has answers here:
Why does sizeof(x++) not increment x?
(10 answers)
Closed 9 years ago.
e.g.
int a = 3;
int b = sizeof(++a);
int c = a;
Is c equal to 3 or 4 as the result?
Does the result depend on the specific compiler?
The specification states that the increment operator does NOT take affect when used within a sizeof operator.
This also makes sense from an abstract viewpoint. Specifically, the sizeof operator returns the number of bytes used by an object. While incrementing an integer or even a pointer does NOT change the size of that integer, the ++ operator would mis-lead a new programmer into thinking the size does change.
If interested look-up the topic "side-effects" for more discussion about this subject.

Resources