delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/07/20/11:50:25

Message-ID: <379494F5.29F4E6B8@geocities.com>
From: Sahab Yazdani <beyonder69 AT geocities DOT com>
X-Mailer: Mozilla 4.51 [en] (Win98; I)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: shifting left, and shifting right...
References: <37937D02 DOT 5D28D561 AT geocities DOT com> <932459103 DOT 734920 AT kyle DOT inet DOT net DOT nz>
Lines: 87
Date: Tue, 20 Jul 1999 11:25:41 -0400
NNTP-Posting-Host: 209.5.16.197
X-Complaints-To: abuse AT sprint DOT ca
X-Trace: newscontent-01.sprint.ca 932484410 209.5.16.197 (Tue, 20 Jul 1999 11:26:50 EDT)
NNTP-Posting-Date: Tue, 20 Jul 1999 11:26:50 EDT
Organization: Sprint Canada Inc.
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019