• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Marie Day
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Paul Clapham
Sheriff
Posts: 22521
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marie Day
Greenhorn
Posts: 29
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks so much, that answers my question.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!