Assume a language's division operator (/
) rounds towards -infinity
.
Other than potentially increased performance, is there any use in the language including an arithmetic shift operator (>>
, say), given that x >> y
would be equivalent to x / (2 ** y)
?
Yes - it's useful to make it clear that you're interested in bitshifting operations; that you're thinking of a value as "a sequence of bits" rather than a magnitude.
For example, if I'm trying to represent a 32-bit integer in four bytes (and I don't have a simpler way of doing it) I might use:
bytes[0] = value & 0xff;
bytes[1] = (value >> 8) & 0xff;
bytes[2] = (value >> 16) & 0xff;
bytes[3] = (value >> 24) & 0xff;
That represents what I'm trying to do much more readably in my opinion than using the equivalent division operations.
See more on this question at Stackoverflow