Author Topic: And-ing  (Read 4937 times)

0 Members and 1 Guest are viewing this topic.

Offline Storm Trooper

  • C= 64
  • **
  • Posts: 45
  • Karma: 2
    • View Profile
And-ing
« on: June 14, 2010 »
have scene this in programs a fair bit with using colours to do effects with in for loops.

I know that it is used for limiting.

I wouldnt mind knowing is what is doing, and what would be an equivalent to do it the long way ( i guess ) with if's.
a few examples.

x and 255
y and 255
col and $ff
col and $ff00
col and $ffff00

col=pal( texture(x,y)) and 255

whilst im online, say I have a colour palette that is 640. is it as simple as:
col=pal( image(x,y)) and 639?

thanks tons,
St0rm Tr0oper.

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: And-ing
« Reply #1 on: June 14, 2010 »
"x AND mask" keeps all bits of x where mask is 1 and clears the others.
In case of '255', or 'FF' in hex, or '11111111' in binary, the lower 8 bits are kept, all other become zero.
It's the same as a "modulo 256" but is magnitudes faster because the modulo-operation requires a division.
In image-processing you'll usually have the r,g,b,a-values of a pixel stored in a single integer:
Code: [Select]
AAAAAAAA:RRRRRRRR:GGGGGGGG:BBBBBBBBTo extract a single color-component you can remove the others by anding with $000000FF, $0000FF00, $00FF0000 or $FF000000.
(but keep in mind that your value is still "shifted up").

Quote
say I have a colour palette that is 640. is it as simple as: col=pal( image(x,y)) and 639?
No, it just works with powers of two (you can only keep "whole" bits).
So just make your palette 512 or 1024 entries long and it works again.
« Last Edit: June 14, 2010 by hellfire »
Challenge Trophies Won:

Offline Storm Trooper

  • C= 64
  • **
  • Posts: 45
  • Karma: 2
    • View Profile
Re: And-ing
« Reply #2 on: July 06, 2010 »
thankyou very much for that.

Offline Storm Trooper

  • C= 64
  • **
  • Posts: 45
  • Karma: 2
    • View Profile
Re: And-ing
« Reply #3 on: July 17, 2010 »
I want to extract the red green and blue in values of 0-255 for each.
Im not sure on which 'SHR w/ AND value' to use. :(

color=src_buffer[ index ]

r=((col shr 16 ) and &hff)
g=((col shr  8 ) and &hff00)
b=((col shr  0 ) and &hffff00)

r_bytes[ index ]=r
..ect.

Thanksyou.

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: And-ing
« Reply #4 on: July 17, 2010 »
Each color-component occupies one byte.
You're now shifting it down to be in the lowest byte so all you need to do is to clear the upper bytes:
Code: [Select]
r=(col shr 16 ) and &hff
g=(col shr  8 ) and &hff
b=col and &hff
The other way round works, too:
Code: [Select]
r=(col and &hff0000) shr 16
g=(col and &hff00) shr  8
b=col and &hff
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: And-ing
« Reply #5 on: July 17, 2010 »
Let's say your 32 bit integer looks like this, as chunks of 8 bits:
A:R:G:B
Each of those 8 bits looks like this in binary
11111111000000000000000000000000
00000000111111110000000000000000
00000000000000001111111100000000
00000000000000000000000011111111
I'm calling these 'masks'
In hex, those numbers are
0xff000000
0x00ff0000
0x0000ff00
0x000000ff
So, you can isolate the colour components by ANDing those binary masks with the original integer.
The truth table for AND is:
Code: [Select]
0 AND 0 is 0
0 AND 1 is 0
1 AND 0 is 0
1 AND 1 is 1
For any bits in the mask which are 1 you will see the original bits from the colour, and any place where the mask is 0, all the original bits will end up 0.
So, there are 2 ways to get the numbers you want:
You can mask using the above masks to isolate the bits you want, then shift the 0s off the end:
Code: [Select]
a=(pix&0xff000000)>>24;
r=(pix&0x00ff0000)>>16;
g=(pix&0x0000ff00)>>8;
b=(pix&0x000000ff);
Or you can shift out all the bits you don't want, and just mask out the lower 8 bits:
Code: [Select]
a=(pix>>24)&0x000000ff;
r=(pix>>16)&0x000000ff;
g=(pix>>8)&0x000000ff;
b=(pix)&0x000000ff;
The results are both the same.

Jim

<edit: apologies for all the edits, something inside smf has broken badly and some text can't be posted >
« Last Edit: July 17, 2010 by Jim »
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: And-ing
« Reply #6 on: July 20, 2010 »
It's not a problem with SMF Jim, it's a problem with software on the server.
Mod_Security thinks that something malicious is being posted, even though it isn't.

The solution aint pretty. Xilo are very security conscious and would probably not remove it, the thought of moving the site to a new and unproven host scares the bejesus out of me.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: And-ing
« Reply #7 on: July 20, 2010 »
It's OK, I had to use 'is' instead of '=' after the AND.  It must think I'm trying to inject some SQL :)

Jim
Challenge Trophies Won:

Offline Storm Trooper

  • C= 64
  • **
  • Posts: 45
  • Karma: 2
    • View Profile
Re: And-ing
« Reply #8 on: July 21, 2010 »
thanks all for the feedback and your wisdom.
not to do with this topic, but Im not able to give karma.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: And-ing
« Reply #9 on: July 21, 2010 »
You need to make 25 posts before you can give Karma points.
Jim
Challenge Trophies Won:

Offline Storm Trooper

  • C= 64
  • **
  • Posts: 45
  • Karma: 2
    • View Profile
Re: And-ing
« Reply #10 on: July 28, 2010 »
maybe a bit silly, but when dealing with colours even if not going to dabble with the alpha channel, still use argb / 0xff001122

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: And-ing
« Reply #11 on: July 29, 2010 »
FWIW, I don't think that's silly at all - even if you're not using the alpha channel right now, the best default for it is 0xff (solid).

Jim
Challenge Trophies Won: