Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Why is the shift direction the inverse of C ? Is this an error ? It would be a pretty big mistake.


JS defines three bitwise shifts which work the way C works on signed int32's:

    [ 
        1 << 30 === Math.pow(2, 30),
        1 << 31 === -Math.pow(2, 31),
        0xf0000000 >>> 1 === 0x78000000,
        0xf0000000 >> 1 === 0x78000000 - Math.pow(2, 31)
    ];
    // ==> [true, true, true, true]
As pointed out elsewhere in this thread there is one exception to this rule:

    0xf0000000 >>> 0 === 0xf0000000;
    // ==> true
In other words, right shifting is always unsigned.


It's definitely a typo.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: