www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/30/10:01:34

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Getting Started
Date: Tue, 29 Apr 1997 21:07:55 +0000
Organization: Two pounds of chaos and a pinch of salt
Lines: 90
Message-ID: <3366632B.76C4@cs.com>
References: <5k5f5g$lkc AT news DOT interlog DOT com>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp107.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Gautam N. Lad wrote:
> 
> 1) What the operators << and >> do and what is their use in programming (If
> this involves converting HEX and/or Binary, I'd like info. on that too).
> 
> 2) What the operator & does and what is it's use in programming

I already explained this in great detail a short while ago - I think it
was to you.  If you want to know what this sort of thing is good for,
well, there are lots of answers.

All of the bitwise operators are useful when dealing with binary
numbers.  In many situations, you will want to represent the attributes
of some object or value with a set of flags, and since all a flag is is
a yes or no value, it's more efficient to use an single bit for each
flag instead of a whole integer variable.  What you do is something like
this:

#define FLAG_1		1	/* binary:  00000001 */
#define FLAG_2		2	/* binary:  00000010 */
#define FLAG_3		4	/* binary:  00000100 */
#define FLAG_4		8	/* binary:  00001000 */
#define FLAG_5		16	/* binary:  00010000 */
#define FLAG_6		32	/* binary:  00100000 */
#define FLAG_7		64	/* binary:  01000000 */
#define FLAG_8		128	/* binary:  10000000 */

struct data_type
{
    int id;
    char *name;
    unsigned long flags;
};

struct data_type data;

/* Use OR operator ('|') to set flags */
data.flags = FLAG_1 | FLAG_4;		/* data == 9 */
data.flags |= FLAG_6;			/* data == 41 */

/* Use AND operator ('&') to test flags */
if ( data.flags & FLAG_4 )		/* 41 & 8 == 8; true */
    printf( "flag 4 set" );

/* Use AND plus NOT operator ('~') to remove flags */
data.flags = data.flags & ~FLAG_4;	/* data == 33 */

This is what you're doing when you use many DOS functions in C; the
open() function takes a set of flags as one of its parameters.

The shift operators ('<<' and '>>') aren't used that often in C
programs, except as substitutes for integer multiplication and division
by powers of two.  For example, x / 2 can also be expressed as x >> 1,
and it will likely be performed much faster that way.  The gcc optimizer
is usually smart enough to convert such operations into shifts for you,
so there's no need to do this.

However, when dealing with composite numbers, such as text mode color
values, shifts can come in extremely handy.  Here, for example, is how
to construct the high byte of a text mode character, which contains the
color values for that character:

unsigned char attr = ( blink << 7 ) + ( bg << 4 ) + fg;

Of course, this could just as easily have been represented by ( blink *
128 + bg * 16 + fg ), but using shifts makes it much more obvious what
you're doing.

> 3) I want to program using registers and interrupts (NO ASSEMBLY -
> PLEASE),  and I would like info on how to use interrupts and registers and what
> they mean and do.

Please look in the libc docs for the DPMI Overview and in chapters 17
and 18 of the FAQ.  This subject is far too complex to discuss on the
mailing list.

> I'd like info. that's available on the 'Net; I do not have books, and I do not plan on
> buying any you recommend either.  However, I may ask my local library to get
> it!

Try Brennan's DJGPP2+Games Resources, at
<http://www.rt66.com/~brennan/djgpp/>.  This site has lots of useful
links and tips.

-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I  |     mailto:fighteer AT cs DOT com     |
| Plan:  To find ANYONE willing to |   http://www.cs.com/fighteer   |
| play Descent 2 on DWANGO!        | Tagline: <this space for rent> |
---------------------------------------------------------------------

- Raw text -


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