Mail Archives: djgpp/1999/07/20/05:00:20

From: "David Mitchell" <dmitchell AT inet DOT net DOT nz>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: shifting left, and shifting right...
Date: Tue, 20 Jul 1999 18:23:07 +1200
Organization: Inet Ltd, Christchurch, New Zealand
Message-ID: <>
References: <37937D02 DOT 5D28D561 AT geocities DOT com>
X-Trace: 932458213 5531568 (20 Jul 1999 08:10:13 GMT)
X-Complaints-To: abuse AT xtra DOT co DOT nz
NNTP-Posting-Date: 20 Jul 1999 08:10:13 GMT
X-Newsreader: Microsoft Outlook Express 4.72.3110.5
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
Cache-Post-Path:!unknown AT p225-tnt1 DOT inet DOT net DOT nz
X-Cache: nntpcache 2.3.3 (see
Lines: 86
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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
        // 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)

00101101        <- LineNum = 2

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 =

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

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 =

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


- Raw text -

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