At what time would a sane person bitshift a signed integer?

2    01 Jul 2016 07:55 by u/Zesty

I only use bitshifting for bit-masks, coming from the gaming industry.

I'm curious, does anybody out there do this? Or is it generally considered "Shit-in-god's-beard-crazy"

5 comments

3

When doing low level stuff where single bits are important. Like micro-controller programming or dealing with a stupid bus.

For anything high level it's less readable and no performance gain since the compiler does it anyway.

0

Ah, I see. So nothing to do with integer math, just manipulating the raw data to be in the right configuration?

0

Some programmers like it to multiply and divide, but I call that bad style because it's less readable.

But when you have to fumble with bits and raw binary data it has its uses where it is more readable.

0

Not shift, but still bit wise. In JavaScript the method indexOf returns -1 when the element can't be found in the array. You can do ~array.indexOf(element) to transform that -1 in zero and anything else in a value different than zero, this way you can use it in an if statement to check for the presence of the element in the array.

0

I'm not exactly sure about shifting signed integers, since you'd have to deal with the two's complement stuff. But for unsigned ints, the only time I've ever shifted was parsing/encoding data formats which either toggled individual bits, or used bits which did not break on byte boundaries for specific values.