• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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

 
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 27371
88
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 30
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks so much, that answers my question.
 
I was her plaything! And so was this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic