PDA

View Full Version : Solved: excels AND function



philfer
10-27-2009, 05:06 AM
I was reading an excel vba book and saw a function which turned a color value ie 16777216 into the RGB format 256 256 256. It used expressions such as :-

ColorValue \ 256 ^ 0 And 255

I thought the And function only returns true or false. In this case how could it perform the transformation required instead of just returning a series of booleans

Any ideas?

Cheers
Phil

Bob Phillips
10-27-2009, 05:43 AM
No, AND is a BIT operator in VB when acting upon numeric values.

1 AND 1 returns 1, any other combination returns 0.

philfer
10-27-2009, 05:46 AM
thanks xld,

but how does it end up returning 256?

Bob Phillips
10-27-2009, 06:27 AM
It can't, the most it can return is 255, which it would if ColorValue is 255.

philfer
10-27-2009, 07:15 AM
Hi,

Thanks for that. Ive seen it return 256 256 256 for the colour black.

If ColorValue is 255 the calculation ColorValue\255 would return 0

0 ^ 0 would return 1 (in fact anything ^ 0 returns 1)

1 And 255 would return 0

Is my logic incorrect. How would it return anything but 0 unless ColorValue is 255 in which case it would return 255.

Thus it seems the only return values are 0 and 255 but I have seen it return all values between 0 and 256???

Bob Phillips
10-27-2009, 08:01 AM
It seems to me to be the same as

ColorValue AND 255

which will return whatever is in ColorValue, but I am only seeing a code fragment

Paul_Hossler
10-27-2009, 05:48 PM
I think that



1 AND 255 would return 0


would return 1 for bitwise AND operator

1 = 00000001
255 = 11111111
AND = 00000001

The most an 8 bit byte can hold is 255 decimal

If you're seeing 256, you're probably looking at a long (32 bits) or an interger (16 bits), and ADDing (not ANDing) so that 255 + 1 will = 256

Color Hex values are stored as longs

This is a little function to seperate the long color values into their RGB components, and while the 3 array elements are typed as Long, they never exceed 255


Function Color2RGB(c As Long) As Variant
Dim RGB(1 To 3) As Long

RGB(1) = c Mod C256
RGB(2) = (c \ C256) Mod 256
RGB(3) = (c \ C256 \ C256) Mod 256

Color2RGB = RGB
End Function


But as our Distinguished Lord of VBAX says, more of the code, including the variable (DIM) declarations, would be helpful

Paul