What does this "AND" do?

Blitz3D Forums/Blitz3D Beginners Area/What does this "AND" do?

Interama(Posted 2007) [#1]
Sorry for the newbie-stupid post, but I'm used to C-like languages (C# and C++) and I'm having a hard time to understand certain aspects of the BASIC language., lol. What does this AND keyword do on the following line?

rgbc=ReadPixel(x,y,ib) And $00ffffff

Thanks,


Floyd(Posted 2007) [#2]
It is "bitwise and", like & in C.

That code reads a 32-bit ARGB value, sets the eight alpha bits to zero and leaves red, green and blue values unchanged.


Mortiis(Posted 2007) [#3]
For example;
If variable = 0 And Keyhit(1)
    Do something
EndIf 



Dreamora(Posted 2007) [#4]
that actually is exactly wrong Mortiis.
This is a boolean and (true and true -> true) and not a bitwise like the one Interama asks about.


_33(Posted 2007) [#5]
Interama:

It's like saying; I want the value to be an intersection of A and B, and the operation to be binary.

So, if A = 31 and B = 6 for example:

31 in binary is 00011111, and 6 is 00000110
So, the operation will be; give me the intersection of 00011111 and 00000110, then result will be 00000110, as 00000110 is contained withing 00011111.

Another one?
11001001 and 01000100 = 01000000


Koriolis(Posted 2007) [#6]
that actually is exactly wrong Mortiis.
This is a boolean and (true and true -> true) and not a bitwise like the one Interama asks about.
We're in a Blitz3D forum, not a BlitzMAx one. In Blitz3D, "And" is indeed bitwise.


_33(Posted 2007) [#7]
Koriolis, no Dreamora is right. You have to look at the context in which the question was asked in the first post, which is binary. And the context in which Mortiis is explaining is a boolean operation on simple conditional result, like you'll find in an If instruction (in fact). It's got absolutely nothing to do with Blitz3d or BlitzMax, it's a universal computing scheme in all processors and languages.


Koriolis(Posted 2007) [#8]
Well, maybe I didn't read Dreamora's answer the same way as you I guess. "And" is bitwise, and that's it. It's just that in Blitz3D it's the only flavour of "And" we get, and we (easily) use it equally as a boolean operator (as in Mortiis's example). And it's good to know this subtle detail, because this also means that if you do "if Not variable and ..." rather than "if variable = 0 and ...", you may have nasty surprises.
EDIT: as for the fact that it's got nothing to do with Blitz3D or BlitzMax, I have to disagree. The "And" operator is bitwise in Blitz3D, and it's logical in BlizMax. That's quite a difference.
And actually, rereading Dreamora's post I understand the same thing as I did in the first place: he says "And" is a logical operator, which is not true in Blitz3D, and looks very much like he's confused BlitzMax (where "And" is logical) for Blitz3D (where "And" is bitwise). But hey, maybe I'm too nitpicky.


_33(Posted 2007) [#9]
Yep, a lot of tricky results can occur if we "think" of some wacky ways we do If's.

If A = B And C Then

If (A And $FF) > 0 Then


Interama(Posted 2007) [#10]
Thanks very much! It confused me the fact that the same operator was applied both for bitwise and logic AND, but OK.


chwaga(Posted 2007) [#11]

If (A And $FF) > 0 Then


what's that mean?


_33(Posted 2007) [#12]
what's that mean?

If (A And $FF) > 0 Then

Say, A is an INT containing at some point, the value 10 in decimal. In hexadecimal, 10 is 0A.

Let's translate that in the If statement

If ($0A And $FF) > 0 Then

"$0A and $FF" basically says; "keep only what is included in the two values. So, for easy understanding, let's look at the binary equation:

00001010 ($0A)
11111111 ($FF) AND
========
00001010 ($0A)

Yep, so it kept everything of what was in A, remember? $0A = decimal 10...... So here's the simplified If statement:

If ($0A) > 0 Then

Or, even better.............

If 10 > 0 Then

Tada!