Android Question Bit.AND

Arturs

Member
Licensed User
IHi

Why the code returns false

B4X:
Dim test_1 As Int = 8

Dim test_2 As Int = 8

Log (Bit.And(test_1,test_2))
Regards
Artur
 

James Chamblin

Active Member
Licensed User
might be better to do Log (Bit.and(test_1,test_2) <> 0) for a result more consistent with other languages, where a 0 results in false and all other values result in true, so test_1 = 8 and test_2 = 12 will still result in true.
 

James Chamblin

Active Member
Licensed User
It is normal that Bit.And(12,8) = 8 is True. If you compare to 0, then it is not necessary to use this operator. Just check test_1 = test_2.
But Bit.And(8,12) = 12 will result in False. In most languages that have an implicit Int to Boolean conversion, both ways would result in True.
B4X:
'Most Languages
If (8 & 12) Then 'True
If (12 & 8) Then 'True
If (8 & 16) Then 'False as 8 AND 16 = 0

'Method #1
If Bit.And(12,8) = 8 Then 'True
If Bit.And(8,12) = 12 Then 'False
If Bit.And(8,16) = 16 Then 'False

'Method #2
If Bit.And(12,8) <> 0 Then 'True
If Bit.And(8,12) <> 0 Then 'True
If Bit.And(8,16) <> 0 Then 'False
As you can see, method 2 gives similar results to other languages. Also, Test_1 = Test_2 would not work as Test_1 = 8 and Test_2 = 12 would result in false instead of true.
 

Informatix

Expert
Licensed User
But Bit.And(8,12) = 12 will result in False. In most languages that have an implicit Int to Boolean conversion, both ways would result in True.
B4X:
'Most Languages
If (8 & 12) Then 'True
If (12 & 8) Then 'True
If (8 & 16) Then 'False as 8 AND 16 = 0

'Method #1
If Bit.And(12,8) = 8 Then 'True
If Bit.And(8,12) = 12 Then 'False
If Bit.And(8,16) = 16 Then 'False

'Method #2
If Bit.And(12,8) <> 0 Then 'True
If Bit.And(8,12) <> 0 Then 'True
If Bit.And(8,16) <> 0 Then 'False
As you can see, method 2 gives similar results to other languages. Also, Test_1 = Test_2 would not work as Test_1 = 8 and Test_2 = 12 would result in false instead of true.
I deleted my message because I answered too quickly. Here's my answer:
might be better to do Log (Bit.and(test_1,test_2) <> 0) for a result more consistent with other languages, where a 0 results in false and all other values result in true, so test_1 = 8 and test_2 = 12 will still result in true.
No, because you don't know what you are checking by comparing to 0. When I write the following, I check explicitely test_2.:
Bit.And(test_1,test_2) = test_2
In your case, I don't know.
 
Last edited:

udg

Expert
Licensed User
Hi all,

maybe I am understanding it wrong, but it seems to me you are discussing on how to use the result from Bit.AND that IMHO should be up to the programmer.
If we look at Bit.AND alone, we all agree that Bit.AND (8,12) returns 8 and so for Bit.AND(12,8).
Now, how we use that returned value 8 should depend on the expected logic in our code. I mean that sometimes we'd like to know whether is zero or not, others if it equals the first or the second parameter and others more if it equals a different number at all.
Think about I/O pin testing. I may be interested at knowing whether one, the other or both pins 2 and 3 (numbering them from 0) are set.
So I'd write a function where I compare the AND result with what I need, passed as a parameter.

That's my 2 cent. If I miserunderstood you, sorry.

udg
 

James Chamblin

Active Member
Licensed User
Example:
Log(Bit.AND(12,8) = 12)
Log(Bit.AND(12,8) <> 0)

Result:
False
True (why true?)
Since we don't know what Artur is trying to accomplish, it is quite possible that your solution is perfect for his needs; however, it is not the way most languages behave. In most languages, you can have If Bit.and(8,12) Then ...
Bit.and(8'12) will evaluate to Int 8, and there is an implied Int to boolean conversion where 8 = true (all non 0 values convert to true wile 0 converts to false) . Since B4A does not have an implicit Int to Boolean conversion, the best way to mimic that behavior is to compare the integer result with 0. If Artur ia trying to mimic a more standard behavior, then my solution will do that.
It really comes down to what he is trying to accomplish
in the first place.
 

LucaMs

Expert
Licensed User
I would avoid all this, because a simple:

If a = x And b = y Then

is more readable

then I will use the binary operators for their purpose.
 

Informatix

Expert
Licensed User
I encountered many times this case in Java libraries because many Java functions have parameters that are a combination of values. For example the gravity of views or the input type of EditText. When you check whether a given value is set, you check as I explained above, not with <> 0.
It is better that you ease the maintenance of the code by being clear. For me, comparing to 0 is not really understandable at first glance. No?
I don't understand your point with other languages because the reasoning is the same.
 

Informatix

Expert
Licensed User
maybe I am understanding it wrong, but it seems to me you are discussing on how to use the result from Bit.AND that IMHO should be up to the programmer.
Absolutely. It's all about clarity of code.
James Chamblin wrote: "might be better to do Log (Bit.and(test_1,test_2) <> 0)"
I completely disagree with him on that point. It's worse, not better.
 

LucaMs

Expert
Licensed User
I meant: using the block if-then-else directly, instead of:
might be better to do Log (Bit.and(test_1,test_2) <> 0) for a result more consistent with other languages, where a 0 results in false and all other values result in true, so test_1 = 8 and test_2 = 12 will still result in true.
is more readable... etc.
 

James Chamblin

Active Member
Licensed User
Once again, we don't know what the OP is trying to accomplish. We don't know why he is doing a bitwise AND on test_1 and test_2 and expecting a boolean as a result. Until we know why, any solution is just as valid as any other. That's why I qualified the word "better" with "might" in my original post. It is just as valid of an assumption he may be trying to convert code from another language to b4a. For example, he might have come across something like this in VB

If (Test_1 And Test_2) Then
DoSomething()
Else
DoSomethingElse()
End If

If he is trying to convert code like that into B4A using informatix's solution, then it will fail to behave the same, whereas my solution will behave exactly the same. Readability means squat if the code doesn't work.

So until we know exactly what Artur is trying to accomplish, we need to stop saying who is "wrong" and who is "right." They are simply different solutions for different purposes.
 

Informatix

Expert
Licensed User
If (Test_1 And Test_2) Then
There's a confusion here between logical operators and binary operators...
It is clear in the first post that the values are not booleans, and the expected result neither.
Whatever you want to achieve, clarity of code will be always better if performance is not of concern.
 
Top