Check that a bitmask flag is zero [duplicate] - objective-c

If I use this:
if(value & 4) to check if the bit is set, then how do I check if the bit isn't set?
I tried with
if(!value & 4) or if(~value & 4) and if(value ^ 4) but none of them works.

When you write if(value & 4), C checks the result to be non-zero. Essentially, it means
if((value & 4) != 0) {
...
}
Therefore, if you would like to check that the bit is not set, compare the result for equality to zero:
if((value & 4) == 0) {
...
}

You could do it many ways, but the easiest (easiest as in requires the least amount of thought) would be just negate the entire expression you already have:
if (!(value & 4))

Simply:
if ((value & 4) == 0)
Why?
If value is 01110011
Then
01110011
&
00000100
--------
Will return 0 because 4th bit is off.

the line from hastebin is poorly written, has unreachable code and depends heavily on the Precedence of the C operators. And doesn't work as expected.
The line from hastebin:
if( cur_w > source.xpos + source.width
&&
!(source.attributes & DBOX_HAS_SHADOW) )
{
break;
return;
}
it should be written as:
if( (cur_w > (source.xpos + source.width)) // has curr_w exceeded sum of two other fields?
&&
((source.attributes & DBOX_HAS_SHADOW) != DBOX_HAS_SHADOW ) //is bit == 0?
{
break;
}

Related

how do i correctly use >= and <= in code?

I have tried many thing involving this, >=, >==, =>, ==>.i can not find one that works. hey all return either primary expression needed or expected initializer before '>'. I am creating a IR receiver latch switch and thus have to create parameters for the code because the receiver is not constant in all conditions. Full code below. Any suggestions to fix the code please reply and don't DM me. Thank you.
code:
int LEDState = 0;
int LEDPin = 8;
int dt = 100;
int recieverOld ==> 500 and recieverOld ==< 2000;
int recieverNew;
int recieverPin = 12;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LEDPin, OUTPUT);
pinMode(recieverPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
recieverNew = digitalRead(recieverPin);
if((recieverOld >== 0 && recieverOld <== 10) && (recieverNew >== 500 && recieverNew <== 2000) {
if(LEDState == 0) {
digitalWrite(LEDPin, HIGH);
LEDState = 1;
}
}
recieverOld = recieverNew;
delay(dt);
}
error:
expected initializer before '==' token
if one = used line 4 and related, return error expected primary-expression before '>' token
if > before = line 4 and related, return error expected initializer before '>=' token
Any solutions or suggestions welcome.
TL;DR
Operators that do no exist, and that you should NOT use:
==>, ==<, >==, <==
Operators that works and you can use them:
>= - MORE THAN OR EQUAL, compare operator, for example X >= 5
<= - LESS THAN OR EQUAL, compare operator, for example X <= 5
> - MORE THAN, compare operator, for example X > 5
< - LESS THAN, compare operator, for example X < 5
== - compare operator, when you want to compare values of the variables if they have the same value, for example X == 5, Y == X, 10 == 7
=== - equality operator, similar to compare operator ==, but aditionally checks the type of a variable. for example X === Y, '10' === 10
= - assign operator, when you want to assign something to the variable, for example X = 5
<> OR != - NOT EQUAL, compare operator, for example X != 5, Y <> 10
!== - similar to != or <>, but also checks the type of a value. For example 10 !== '10', and will return opposite result of the equality operator ===

Check for consecutive value in Map?

I'm creating a chat app and don't want to display the user avatar over and over if a user sends multiple messages consecutively.
The user messages are stored in a Map, where the newest message has the index 0.
To check if a message at an index is sent by the same person as the message before, I use the following method:
bool _sameUser () {
if (index > 0 && map != null && map[index + 1] != null && map[index + 1]['fromUser’] == map[index][‘fromUser’]) {
return true;
} else {
return false;
}
}
However, this doesn't work for the newest message or if there are more than two messages from the same user.
How I can rewrite the conditional so it works as intended?
Thanks!
UPDATE:
After try #Marcel answer and test more I find another issue maybe cause this.
map[index + 1] != null is not true even when message 2 is send. Is only true after message 3 is send.
I test with conditional in function:
if (map[index + 1] != null) {
print('!=null');
}
There's no reason your code shouldn't work for the index 0 except you test index > 0.
By removing that, it should work fine:
bool _sameUser () {
assert(index >= 0);
assert(map != null);
return map[index + 1] != null && map[index + 1]['fromUser'] == map[index]['fromUser'];
}
Because I assumed, the index should never be smaller than 0 and the map should never be null, I moved some of the condition code to assert statements.
Also, because your if-expression is a boolean, you can just return it directly.

How can you capture modifier keys exclusively?

When using NSEvent flagsChanged and ANDing the flags with various KeyMasks, how can you test themin an exclusive way?
Currently, using a series of if else conditions whit the pattern:
if ((flags & someKeyMask) && (flags someOtherKeyMask))
This will match even if a third modifier key is down.
Putting longer series of key masks earlier in the if else conditionals makes the behavior work as desired, but feels incomplete somehow. Is the a good way to say "only these modifier keys, not any others"?
Here is a more specific example where the first one matches before the others. I'm wondering if there is a way to add some logic to each one that says "only these modifier keys".
if ((flags & (NSCommandKeyMask|NSControlKeyMask))) {
NSLog(#"one");
}else if (((flags & NSCommandKeyMask) && (flags & NSAlternateKeyMask)) && (flags & NSControlKeyMask)) {
NSLog(#"Command+Option+Control ");
} else if ((flags & NSCommandKeyMask) && (flags & NSShiftKeyMask)) {
NSLog(#"Command+Shift ");
} else if ((flags & NSCommandKeyMask) && (flags & NSControlKeyMask)) {
NSLog(#"Command+Control");
} else if ((flags & NSCommandKeyMask) && (flags & NSAlternateKeyMask)) {
NSLog(#"Command+Option ");
}
So the correct pattern I was looking for, as provided by Ken Thomases is:
flags &= (<one or more masks bitwise OR'd together);
if (flags == (<one or more masks bitwise OR'd together)) { // do something }
This gives exclusive matching.
First, you need to be aware that the value returned from -modifierFlags includes some flags which do not exactly correspond to keys. You should construct a mask which includes all of the flags that you care about (whether you care that they are pressed or not pressed). Pass the flags value through that mask and then compare the result with exactly the combination you want.
For example, if you care about Command, Option, Shift, and Control, and you want to know if exactly Command and Shift are down but the others are not, you could use:
if ((flags & (NSShiftKeyMask|NSControlKeyMask|NSAlternateKeyMask|NSCommandKeyMask)) == (NSShiftKeyMask|NSCommandKeyMask))
// do something
Update: Here's how to check a variety of combinations:
flags &= NSShiftKeyMask|NSControlKeyMask|NSAlternateKeyMask|NSCommandKeyMask;
if (flags == (NSControlKeyMask|NSAlternateKeyMask|NSCommandKeyMask))
NSLogs(#"Command+Option+Control");
else if (flags == (NSShiftKeyMask|NSCommandKeyMask))
NSLog(#"Command+Shift ");
else if (flags == (NSControlKeyMask|NSCommandKeyMask))
NSLog(#"Command+Control");
else if (flags == (NSAlternateKeyMask|NSCommandKeyMask))
NSLog(#"Command+Option ");
In order to catch just the combination pressed, you need to use the switch/break construction:
switch (flags) {
case (NSControlKeyMask|NSAlternateKeyMask|NSCommandKeyMask):
[keystrokes appendString:#"cmd-alt-ctrl-"];
break;
case (NSShiftKeyMask|NSCommandKeyMask):
[keystrokes appendString:#"cmd-shift-"];
break;
case (NSControlKeyMask|NSCommandKeyMask):
[keystrokes appendString:#"cmd-ctrl-"];
break;
case (NSAlternateKeyMask|NSCommandKeyMask):
[keystrokes appendString:#"cmd-alt-"];
break;
default:
break;
}

if statement gone wrong -xcode

Guys what am I doing wrong?
if (numberstring.intValue <=15) {
rankLabel.text = #"A1";
}
else if (numberstring.intValue >16 && <=40){
rankLabel.text = #"A2";
}
I get an error on the "<=40" ..
You missed off a variable reference:
if (numberstring.intValue <=15) {
rankLabel.text = #"A1";
} // vv here vv
else if (numberstring.intValue >16 && numberstring.intValue <= 40){
rankLabel.text = #"A2";
}
As an optional extra, it looks like numberstring is an NSString object, which you are repeatedly converting to an integer in order to test various ranges. That operation is quite expensive, so you are better off doing the conversion once:
int value = [numberstring intValue];
if (value <=15) {
rankLabel.text = #"A1";
}
else if (value >16 && value <= 40){
rankLabel.text = #"A2";
}
Also note that the intValue method is not a property so I would avoid using the Objective-C 2.0 dot syntax to access it and use the normal method calling mechanism.
The && operator links two clauses together. However, each clause is independent, so each one has to be syntactically correct on its own if the other was removed. If you apply this rule to your condition, you can see that "<=40" is not syntactically correct on its own. Thus you need to reference the value being compared, as follows:
if (numberstring.intValue > 16 &&
numberstring.intValue <= 40) // this is syntactically correct on its own

Why does the Objective-C Compiler return a "y" as a "0" but then skips over the "if input==0" section?

int side1test;
NSLog(#"Is your triangle setup as in an Angle-Side-Angle? (Use 1 for Yes and 0 for No.)");
scanf(" %i", &side1test);
Returns "0" when the user enters a "y." However,
if (side1test != 1 && side1test != 0){
NSLog(#"Please use a '1' for YES and '0' for NO.");
}
Then does not catch.
The program drops into my else clause, and outputs all the NSLogs, skipping the scanf() commands, taking each of them as "0." What is wrong here?
I'm not a c++ dev but from googling that function returns the number of valid matches. If it returns 0 you should assume invalid input. side1test has not been set which is why it's 0.
Your code should probably be:--
int side1test;
NSLog(#"Is your triangle setup as in an Angle-Side-Angle? (Use 1 for Yes and 0 for No.)");
int result = 0;
while (result==0)
{
result =scanf(" %i", &side1test);
}
if (side1test != 1 && side1test != 0){
NSLog(#"Please use a '1' for YES and '0' for NO.");
}