• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How would I do this in Java? Union to split unsigned int into two signed short's.

 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How would I achieve this properly in Java code?


[ October 31, 2008: Message edited by: Dan Bizman ]
 
author
Posts: 23951
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
First of all, Java doesn't support unsigned ints. Second of all, Java doesn't support unions... so... you kinda have to make due with signed ints and dealing with extracting the bit patterns from it.

Now, having said that... using shifting along with the AND/OR operators is not a very efficient way to convert an int into two shorts. The fastest way to convert an int into two shorts is probably via a byte buffer...



For the reverse process, I left it as an exercise for you to figure out...

Henry
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:
First of all, Java doesn't support unsigned ints. Second of all, Java doesn't support unions... so... you kinda have to make due with signed ints and dealing with extracting the bit patterns from it.

Now, having said that... using shifting along with the AND/OR operators is not a very efficient way to convert an int into two shorts. The fastest way to convert an int into two shorts is probably via a byte buffer...



For the reverse process, I left it as an exercise for you to figure out...

Henry



Thanks! That's great, I never knew about that. I like how the use of the byte buffer is as clean as the union usage! I'll experiment doing the reverse.
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

using shifting along with the AND/OR operators is not a very efficient way to convert an int into two shorts.

Out of curiousity, in what way do you think it is inefficient? I would predict it would be theoretically faster than what you suggest:where the "(short)" effectively does "&0xFFFF" and allows the Java compiler to accept the result in the array.

My approach requires: one bit shift, which on a barrel shift CPU like the Intel Core and Core 2 series takes only a couple of cycles, and two AND masks which take a cycle each, plus overheads for moving data and masks around the registers.

Compare with using a ByteBuffer, which requires allocation (and may have JNI overheads for a direct buffer and heap overheads for a new instance) and four method invocations each which alter the calling stack as overheads. So which is more efficient? And which in fact is more compact?

If it were me, I'd just write a union style class:
 
Henry Wong
author
Posts: 23951
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

My approach requires: one bit shift, which on a barrel shift CPU like the Intel Core and Core 2 series takes only a couple of cycles, and two AND masks which take a cycle each, plus overheads for moving data and masks around the registers.



This is actually a good point. I guess my "old timer" instincts are still in play here. I keep forgetting that with newer intel processors, the timing for the shift instructions are no longer dependent on the number of shifts.

Henry
 
Charles Lyons
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, the barrel shift was first introduced in the Intel 386 in the mid 80s, so it's nothing particularly new... They could do a variable length shift in just one cycle, and so could the later 486 and Pentiums. Then in the P4 Intel simulated the shift instructions in microcode rather than with circuitry---making the P4 much slower for shifts (4 to 6 cycles rather than just one). With the Intel Cores they returned back to the old model... weird.

Even with a P4 using the shifts must be faster than the ByteBuffer due to the instantiation and method calls involved in the latter, and the relatively large processing (well over 6 cycles) and memory overheads of each. Even function calls in C can be noticeable in realtime apps, hence the "inline" keyword. Indeed, if I could in Java, I'd mark the getter methods on the class I created above "inline".

Of course, it's all on a small scale and a regular OS will impose so many other services and thread yields that it's probably negligible. If the ByteBuffers are easier for what you're doing then use them. If you were transforming a lot of network data for example, it might be more convenient to use a ByteBuffer since the network bandwidth and not the CPU will most likely be the bottleneck. They can also be useful in direct mode for linking up efficiently with native JNI components.
 
Henry Wong
author
Posts: 23951
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

Actually, the barrel shift was first introduced in the Intel 386 in the mid 80s, so it's nothing particularly new...



For the record, my Intel assembly coding tenor started in the Mid 80s, with the 8088 / 8087, til almost 1990, with the 80286. I also did about 6 months with the Sparc assemby during the mid 90s.

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic