• 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

Why is this AND operation necessary?

 
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

The purpose of this code is to store two bytes of a short sequentially in the buffer.
Why does this have to be an AND operation?

Couldn't you just cast short ss as a byte? The significant byte would be cut off, and the
other byte would be stored in the buffer, wouldn't it, without needing the AND operation?

I must be missing something. Code below.

This code is from
http://www.drdobbs.com/jvm/music-components-in-java-creating-oscill/230500178

 
Saloon Keeper
Posts: 9558
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'ss' is a short which takes two bytes of storage, 0xFF has 1 byte's worth of bits turned on (1111 1111), doing a bit wise AND then keeps only the least significant byte of the two bytes of the short.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marie Day wrote:Couldn't you just cast short ss as a byte? The significant byte would be cut off, and the
other byte would be stored in the buffer, wouldn't it, without needing the AND operation?

I must be missing something.


You are right, the AND operation to keep only the lower 8 bits of the short is not necessary, because casting a short to a byte already does this. So it could just have been:


Maybe the programmer who wrote this didn't understand this properly.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:
Maybe the programmer who wrote this didn't understand this properly.



Or perhaps, it is done for consistency. For example, to go from short to byte, you need the explicit cast, but not the AND operation. And to go from byte to short, you don't need the explicit cast, but you do need the AND operation... perhaps, it is just easier to make sure that each conversion has the cast and masking of bits, regardless whether it is needed or not.

Regardless, it is an interesting catch, and cow-worthy. Have a cow...

Henry
 
Marie Day
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

Thanks for the cow, and clarification.
 
If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic