Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

alternative to "ints" (?)

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I was reading an article on www.gamedev.net, about memory usage for an application. It stated this

if a particular type of data only requires 3-bits of information try to use a bitfield (or least only 1 8-bit byte or unsigned char) instead of just allocating an "int" (32-bits) to hold it.

I understand what it says, but does this relate to java at all, and if so, is there a way in java to do this? Maybe java only uses the required space and doesnt take into consideration unused space?

thankyou

Seamus
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look into the other primitive types. A short and a byte each use fewer bits than an int. It's been awhile since I studied this but if you google for it you should find some information.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two points. Using a smaller data type may not actually use less memory. On many languages and many platforms, it is inefficient to use less than one word (32 bits on a 32-bit platform, 64 on 64 etc.) for a piece of data, so they don't. In Java, you have a guarantee that a "byte" always behaves as having 8 bits and an "int" always behaves as having 32, but I believe there is no guarantee about how much physical memory they will use, and I believe they will use exactly the same amount, on some platforms.

Second, while those working on platforms with very limited resources (e.g. mobile devices), or where performance really is key (e.g games) do want to squeeze as much data as possible into a small space, most other applications don't care and shouldn't bother. It is much more important, for most applications, to write your code clearly, concisely and robustly, than to save a few bytes of memory. Modern machines generally have plenty!
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
He,

Originally posted by Peter Chase:
It is much more important, for most applications, to write your code clearly, concisely and robustly, than to save a few bytes of memory. Modern machines generally have plenty!



I totally agree with Peter here. I never used them but isnt BitSet usefull for those situation???
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jean-Sebastien Abella:
He,


I totally agree with Peter here. I never used them but isnt BitSet usefull for those situation???



Okay, normally I would agree with you guys, but I take issue with this because you're assuming that we all build apps on PCs with enough RAM. However, when developing apps on wireless devices, you have to think about memory usage, and saving a few bytes can make all the difference.
[ August 11, 2005: Message edited by: Richard Anderson ]
 
Jean-Sebastien Abella
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Richard Anderson:


Okay, normally I would agree with you guys, but I take issue with this because you're assuming that we all build apps on PCs with enough RAM. However, when developing apps on wireless devices, you have to think about memory usage, and saving a few bytes can make all the difference.



What would you suggest then?
The use of bit mask and shift on int or long, or maybe a BitSet?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jean-Sebastien Abella:


What would you suggest then?
The use of bit mask and shift on int or long, or maybe a BitSet?



I think it all depends on the exact problem you are trying to solve. Different situations with different requirements will require different solutions. Either one may be useful depending on the situation.

One thing you may need to consider is that BitSet is a class. As such, you will need 4 bytes to hold a reference to a BitSet object plus the BitSet internally needs memory to store its value. If you are only using 3 bits, then this doesn't seem to save anything over an int.

Layne
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Richard Anderson:


Okay, normally I would agree with you guys, but I take issue with this because you're assuming that we all build apps on PCs with enough RAM. However, when developing apps on wireless devices, you have to think about memory usage, and saving a few bytes can make all the difference.

[ August 11, 2005: Message edited by: Richard Anderson ]



Perhaps you should go back and read Peter's original post again. He did mention that memory usage is an issue on limited devices.

Layne
 
Jean-Sebastien Abella
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But peter is not the one asking!
 
Seamus GalIagher
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

it was me asking :-) i was just curious. However, one of the replies did mention memory in smaller devices. In japan especially, mobile phone games have taken off.. Perhaps newer techniques, or more emphasis on how information can be stored as best as possible, is becoming more of a focus.
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if a particular type of data only requires 3-bits of information try to use a bitfield (or least only 1 8-bit byte or unsigned char) instead of just allocating an "int" (32-bits) to hold it.


Store that data in a byte which uses eight, count 'em, bits. Then write a routine to return tribbles. A tribble is three bits, whereas a nibble is four bits � a dibble is two bits for example. Nibble is an official term, the other two I just made up because they sound neat.

Anyway, three bytes holds eight tribbles so you probably want to round off your tribble arrays to three bytes or anything modulo three.

Mext you will need a routine to address a tribble: set and get from an array where the index is the tribble index. The return and input values are bytes, so you waste five bits but only when passing around individual tribbles.

I will leave the implementation as an exercise for your own personal enjoyment but it goes something like this: locate the byte, isolate the tribble within that byte with a shift, mask off the high bits:

public void getTribble(int index)
{
int slot = index * 3 / 8;
int offset = index * 3 % 8;
return tribbles[ index ] >>> offset & 3;
}
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like that, Rick. I did similar things in the Carter & Reagan years in 370 asembler and Turbo Pascal. I used another language with 4 bit digits (0-9) and 6 bit letters. Nothing had to be on any boundaries so you could mix & match at will.
 
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



public void getTribble(int index)
{
int slot = index * 3 / 8;
int offset = index * 3 % 8;
return tribbles[ index ] >>> offset & 3;
}



That's not going to work. It won't even compile.

Guy
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> That's not going to work. It won't even compile.

It works perfectly: it's pseudo code which I warned about it the preface.

I just fixed it up with the casts and the return value, compiled it and ran it. To my surprise it worked! Needs paint however: boundary conditions won't let you use it as is, even with the corrections.

Anyhoo, the idea was not to provide the implementation but the general pattern and a general outline as a special bonus prize.
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rick -

"Dibbles" and "Tribbles" ... love 'em!

Another issue is the arithmetic concersion issue. If two dibbles, represented as bytes, are to be multiplied, Java will convert the bytes to ints. Then after the multiplication, the result has to be cast back to byte. If arithmetic isn't carefully managed, all the conversion and casting could chew up a lot of time.

Thinking out loud here, I would be tempted to use lookup tables for arithmetic. Suppose I want to multiply a pair of dibbles, one in bits 1:0 of its byte and one in bits 3:2 of its (different) byte. Assuming all other bits are zero (that is, the other dibbles or tribbles have been masked out and I'm now dealing with ints), I would add the dibbles and use the result as an index into a byte array. I would have one array for addition results, one for subtraction, etc.

I'm really enjoying typing "dibble" and "tribble" so I'm going to do it once more before signing off. Dibble. Tribble.

-- Phil
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Philip Heller:
"Dibbles" and "Tribbles" ... love 'em!



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