Forums Register Login

Doesn't bit shifting change the value of the variable?

+Pie Number of slices to send: Send
I'm reading code for creating a digital oscillator. This part is storing sample values in a buffer.
On the line

short ss is shifted to the right, and then ss is used again on the next line.

The purpose of the code is to store two bytes of a short sequentially in the buffer,
but for the code to make sense wouldn't ss have to have it's original value on both lines, which means
the value of ss was not changed by >> 8? Otherwise, the first line gets rid of the least significant byte.

So, bit shifting operations don't change the value of the variable? That's my question.
Here's all the code. Comments are mine. Here's the source of the code:
http://www.drdobbs.com/jvm/music-components-in-java-creating-oscill/230500178

1
+Pie Number of slices to send: Send
No, shifting operations don't change the value of the variable they are applied to. Neither do arithmetic operations: a * 10 produces the value of a multiplied by 10 as its value, but it doesn't change the value of a at all. In fact the only operations which change the value of the variable they are applied to are the two increment operations (represented by ++) and the two decrement operations (represented by --).

And if you play around with code you'll notice that most operations can be applied to things other than variables; consider 6 + 7 for example. It produces 13 as its value, but clearly it doesn't change the value of 6. Or consider (a + b) * 10... this is a perfectly ordinary arithmetic expression and it doesn't change the value of any variable either. The only exceptions to this rule are the increment and decrement operations which I mentioned earlier; they can only be applied to variables, because they do change the value of the thing they are applied to.

It's actually more complicated than that, because there's the "=" operation which assigns the value on its right to the variable on its left, and everything I've said here about "variables" can apply to array entries, for example numbers[i] << 3, but hopefully I've addressed your concern.
1
+Pie Number of slices to send: Send

Thanks so much, that answers my question.
Enjoy the full beauty of the english language. Embedded in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1367 times.
Similar Threads
creating echo in audio file
audio visualization graphics
why won't this shift work?
Convert 2 bytes to a a Int
what is a lightweight way to convert double or integers to byte arrays in Java
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:38:49.