🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.modula2
Article #3804

Re: Bit-shifting Operations

#3804
From: Steve Smith
Date: Thu, 03 Sep 1987 22:16
32 lines
2520 bytes
In article <8708271750.AA03034@cayuga.cs.rochester.edu> ken@CS.ROCHESTER.EDU (Ken Yap) writes:

>A good question though is: what do people use bit shifting for in
>C and how would one write the equivalent in M-2? I know I use bit
>shifting to (1) make bitmasks, which M-2 can do with BITSETs and
>(2) (not so much now) multiply and divide, which shouldn't be
>a BITSET operation anyhow. So what can BITSETs not do?

The main use that I've found for bit twiddling, shifting, etc. is a
devilish contraption known as a device regester.  This refers especially
to UARTs, DMA chips, etc, although CPU status regesters have the same
problems.  For their own good (:-) reasons, hardware designers tend to
put the fields you're interested in someplace other than the beginning
of a word.  For single bit values, this is no problem - bitsets are just
great.  However, for multibit numeric values, this gets grubby.  In
assembly language, you just mask off the part you're interested in and
shift it over into the least significant position, where your compare
instructions can handle it sensibly.  In Pascal, you declare your
register as a PACKED RECORD of however you want things handled.  This
should compile into a mask-and-shift just like the assembly version.
(I'm an optomist :-)

I really don't see a clean way of doing a mask, shift, and compare in
M2.  The mask is done easily enough with a bitset, but then how do you
do a sensible compare?  How do you tell that (say) {10, 12} > {10, 11}
without doing a type transfer onto a CARDINAL?  Note that a program full
of type transfers is prime ammunition for the anti-strong typing crowd.
-- 
                           __
 -- Steve          /      /  \      /         "Truth is stranger than
S. G. Smith      I \ O    |  _    O \ I        fiction because fiction
smith@cos.com      /      \__/      /          has to make sense."
Technical Details
Message-ID: <430@cos.COM>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!husc6!mit-eddie!ll-xn!ames!amdcad!sun!texsun!sundc!hqda-ai!cos!smith
References: <8708271750.AA03034@cayuga.cs.rochester.edu>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!husc6!mit-eddie!ll-xn!ames!amdcad!sun!texsun!sundc!hqda-ai!cos!smith
From: smith@COS.COM (Steve Smith)
Newsgroups: comp.lang.modula2
Subject: Re: Bit-shifting Operations
Message-ID: <430@cos.COM>
Date: Thu, 3-Sep-87 18:16:44 EDT
Article-I.D.: cos.430
Posted: Thu Sep  3 18:16:44 1987
Date-Received: Sat, 5-Sep-87 15:31:29 EDT
References: <8708271750.AA03034@cayuga.cs.rochester.edu>
Reply-To: smith@cos.UUCP (Steve Smith)
Organization: Corporation for Open Systems, McLean, VA
Lines: 32
Summary: Uh, oh.  Hardware!