www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/23/12:39:40

From: "John M. Aldrich" <fighteer AT NO DOT SPAM DOT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: C/C++ Question!
Date: Tue, 22 Apr 1997 08:53:24 +0000
Organization: Two pounds of chaos and a pinch of salt
Lines: 55
Message-ID: <335C7C84.78DC@NO.SPAM.cs.com>
References: <5jj7mg$9o4 AT news DOT interlog DOT com>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp103.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Gautam N. Lad wrote:
> 
> Hi,
> I know I should be posting in one of the C or C++ groups, but I'm doing it
> here, 'cause I use DJGPP! ;)
> 
> Anyways, I've come across two modifiers (or whatever they're called) in C/C++.
> One that is >> or << (eg. n>>2 or n<<2), and the other is the single $ (eg.
> if (a & b) ).  What do these mean and what do they do?
> 
> I do not know ASM, or that deep into interrupts (or much of the x86
> architecture), so don't be too advanced!  Just plain ol' C/C++!
> I'm not being harsh here, so don't flame me! ;)

Those thingies are called operators (because they operate on their
arguments (in a computer sense, not a surgical one (<g>))), and the ones
you're talking about are called 'bitwise' operators, because they work
with their arguments as a sequence of bits rather than as numbers.  You
are familiar, I trust, with the binary counting system?  It's sort of
required if you're going to understand how bitwise operators work.

The '>>' operator performs a bitwise shift to the right.  x >> y shifts
x to the right y places, filling in the empty bits with zeroes (if the
number is positive or unsigned) or ones (if the number is negative). 
Thus, 65 >> 2 == 16.

The '<<' operator performs a bitwise shift to the left.  x << y shifts x
to the left y places, filling in the empty bits with zeroes.  Thus, 65
<< 2 == 260.

The shifting operators have a relatively low order of mathematical
precedence, below '+' and '-', but above '<', '>', '<=', and '>='.  They
also, as you can probably see from the examples, serve as a fast way to
multiply or divide by powers of two, although any good compiler will
probably do that when it optimizes.

The '&' (ampersand) operator performs a bitwise AND on its operands. 
The '|' (vertical bar) operator performs a bitwise OR on its operands. 
The '^' (carat) operator performs a bitwise XOR on its operands.  The
'~' (tilde) operator performs a bitwise NOT on its single operand.  The
order of precedence places '~' second from the top as a right-to-left
unary operator (the same as '!').  The '&', '^', and '|' operators, in
that order, are lower than '==' and '!=', and above the logical '&&'
operator.

The above information (and much more) should be part of any standard
ANSI C reference.

-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I      |   mailto:fighteer AT cs DOT com   |
| God's final message to His Creation: | http://www.cs.com/fighteer |
| "We apologize for the inconvenience."| Fight against proprietary  |
|                      - Douglas Adams | software - support the FSF!|
---------------------------------------------------------------------

- Raw text -


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