Mail Archives: djgpp/1999/07/20/11:50:25
thanks for your help, it helped me greatly!
David Mitchell wrote:
>
> Sahab Yazdani wrote in message <37937D02 DOT 5D28D561 AT geocities DOT com>...
> >I'm having some trouble understanding bit-wise operations. Lets take a
> >transparency map as an example, it would be more efficeint to store the
> >transparency map as a pixel per bit bitmap (ie black and white), but I
> >have no clue how to store the individual pixels in the map. Somebody
> >told be that I have to use the shift left and right operators << & >>??
> >Can anybody confirm this and also possibly send some code as to how to
> >do it???
>
> Lets say that you have a map which is 8x8 pixels (unrealistic I know, but
> bear with me)
>
> This means that you can store it in only 8 bytes as opposed to 64.
>
> Right, to do this, you want to be able to set it, and read it.
>
> To read it you would want to say:
>
> if (PixelMap[lineNum] & (1 << column))
> {
> // Then the bit is set
> }
> else
> {
> // It's not
> }
>
> Where PixelMap is an array of unsigned char (in this case of size 8)
>
> How it works is Lets say lineNum is 2, column is 3 (remember origin is 0, 0)
> so your pixelMap looks like this (in Binary)
>
> 00101001
> 10110101
> 00101101 <- LineNum = 2
> 10100101
> 11011010
> 10111101
> 00101101
> 11011001
>
> so the left side of the if statement is 00101101, and the right hand side is
> 1 << column
> so it shifts the binary for 1 left column places.
> When column is 3 this gives: 1 << 3 = 00001000
>
> When you logical and this with 00101101
> then you get:
>
> 00101101 and
> 00001000 =
> ------------
> 00001000
>
> Since this is non-zero we get a true. You will notice that the bit for the
> left hand column is the right hand bit (LSB)
>
> To set it is a little trickier because you don't want to clobber the other
> values
>
> if (!(pixelMap[lineNum] & (1<<column)))
> {
> // It isn't set so set it
> pixelMap[lineNum] += (1 << column);
> }
>
> So with the above values but trying to set column 4
> 00101101 +
> 00010000 =
> -----------
> 00111101
>
> To unset it you just say the opposite
>
> if (pixelMap[lineNum] & (1 << column))
> {
> // It is set, unset it
> pixelMap[lineNum] -= (1 << column);
> }
>
> Hope this helps
>
> David.
- Raw text -