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 u/xyzzy 01 Jul 2016 08:24
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 u/Zesty [OP] 01 Jul 2016 11:30
Ah, I see. So nothing to do with integer math, just manipulating the raw data to be in the right configuration?
0 u/xyzzy 01 Jul 2016 13:07
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 u/Torchhead 01 Jul 2016 22:40
Not shift, but still bit wise. In JavaScript the method
indexOfreturns-1when the element can't be found in the array. You can do~array.indexOf(element)to transform that-1in zero and anything else in a value different than zero, this way you can use it in anifstatement to check for the presence of the element in the array.0 u/RewriteFullwise 02 Jul 2016 22:16
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.