Bitshifting negatives in Pike is a little sketchy
Douglas Gardner <[email protected]>
| Newsgroups | gmane.comp.lang.pike.user |
|---|---|
| Message-ID | <CAPHk0X+reA_ubH+MnBOQrvDUNS+828os+nprGYC48B+Z6=eiOQ@mail.gmail.com> |
Hi all, In Pike 7.8, the following operations happen: 25 >> 1 → 12 -25 >> 1 → -1 25 >> -1 → Bad argument 2 to >>(). Expected int(0..)|object. -25 >> -1 → Bad argument 2 to >>(). Expected int(0..)|object. Only the first one of these is handled correctly. Bitshifting negative integers is a generally sketchy business in many languages, but most seem to agree that -25 >> 1 is -13. However, in Pike, right bitshifting any negative integer results in -1 (which is rather wrong!). 25 >> -1 and -25 >> -1 are a little undecided. In most languages, such as Python, Java and Perl, they return 0. In C and Ruby, a negative right shift turns into a left shift, so they return 50 and -50. In Python and Go, they raise errors. All three approaches are valid, but in Pike we hit a fairly opaque unknown program error -- this should change to match the behaviour of <<(), which is to tell you off about a negative shift count. All in all, we should: * make a negative shifted by a positive work correctly * make a negative shifted by a negative either work sensibly, or tell you off properly Thanks, Douglas