Opened 11 years ago

Closed 10 years ago

Last modified 9 years ago

#1005 closed proposal (wontfix)

Add Logical Shift Operators

Reported by: rob@… Owned by:
Priority: normal Milestone:
Component: libraries/base Version: 6.6
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


This patch adds logical shift operators to complement the current arithmetic shift operators in Data.Bits.

The patch has been tested against GHC on Linux running on Intel 32bit architecture. I haven't had an opportunity to test other architectures but they should work fine.

The patch shouldn't break NHC or Hugs but I haven't been able to test them either. As it currently is, among the Int family, logical shift operators should work on these platforms for Int only. the Int* family of sized integers will currently treat logical shift operators as arithmetic shift operators as I couldn't figure out how to patch these.

Integer will raise an error if a logical right-shift operator is attempted since it doesn't seem to make any sense for this type.

By default, the logical shift operators will act as arithmetic shift operators and so should require no method definition for unsigned types.

The patch also includes quickcheck properties which I've taken the liberty of creating a Tests directory for. I thought it would make sense to keep quickcheck properties with the library they belong to for easier management.

Attachments (3)

log_shift_ops.patch (46.9 KB) - added by rob@… 11 years ago.
log_shift_ops_new.patch (43.6 KB) - added by guest 11 years ago.
bits_test.patch (37.2 KB) - added by guest 11 years ago.

Download all attachments as: .zip

Change History (12)

Changed 11 years ago by rob@…

Attachment: log_shift_ops.patch added

Changed 11 years ago by guest

Attachment: log_shift_ops_new.patch added

Changed 11 years ago by guest

Attachment: bits_test.patch added

comment:1 Changed 11 years ago by malcolm.wallace@…

Could you describe briefly what is the intended difference between arithmetic shift and logical shift?

Also, I don't think it is easy to justify proposing a change to the base package, but only to test it on one implementation platform. Portability is vital!

comment:2 Changed 11 years ago by dons

Check the code works in GHC and Hugs at the very least (the QuickCheck properties should run fine in both environments).

comment:3 Changed 11 years ago by rob

Ok. I'm getting Hugs and NHC. It'll probably take me a while. I think I've found some things that need fixing anyway, so standby for updated patches.

Malcolm: info about logical shift can be found here:

comment:4 Changed 11 years ago by malcolm.wallace@…

Thanks for the pointer to a description of logical vs arithmetic shift. However, this does flag up an assumption in the proposal - namely that the current specification requires arithmetic shifting. As far as I can tell, nothing in the interface documentation for Data.Bits says that the operations should be arithmetic, and indeed I couldn't guarantee that we do not _already_ have logical shifts in either Hugs or nhc98. It might be that it is the arithmetic variants that are missing.

Having said that, I do think it is good to propose that the specification should be more precise. And it would indeed be good to have separate functions for the separate styles of operation. (But implementing them efficiently might conceivably require some architecture-specific configuration.)

comment:5 Changed 11 years ago by Rob

True; I hadn't verified whether the implementations were consistent... I'll have a look around at how other high level languages guarantee their shift operations are arithmetic and change this proposal into something like you suggest.

So how do I take this proposal off the "current" list and put it back on the drawing board?

comment:6 Changed 11 years ago by ross

The FFI Addendum says:

The function shift performs sign extension on signed number types; i.e., right shifts fill the top bits with 1 if the number is negative and with 0 otherwise.

(This text was missing from Data.Bits)

Isn't that reasonable? That is, use Wordn to get logical shifts?

(If you want to scrub this ticket, resolve it as wontfix)

comment:7 Changed 10 years ago by ross

Resolution: wontfix
Status: newclosed

Proposal seems abandoned, and may be unnecessary anyway.

comment:8 Changed 9 years ago by simonmar

Architecture: UnknownUnknown/Multiple

comment:9 Changed 9 years ago by simonmar

Operating System: UnknownUnknown/Multiple
Note: See TracTickets for help on using tickets.