1

I need to extract 'thousands' from an integer (e.g. 1345 -> 1; 24378 -> 24) and since my application is going to do this a lot, I need to do this efficiently.

Of course, division by 1000 is always an option, but division is an expensive operation so I'm looking for something more efficient.

The target platform is Android and as far as I know most Android platforms today do not have a math co-processor, so the most preferred way would be to do this by bit-wise manipulations, though I can't figure out how to do that and if its possible at all...

Kikosha
  • 343
  • 6
  • 16
  • 2
    Are you doing pre-mature optimization? First make sure that anything more efficient than simple integer division is going to make any difference to performance of your application. – Ashwinee K Jha Dec 04 '11 at 12:43
  • 1
    Just use inline assembler. Oh wait, it's Java? Why care about the performance of an integer division then? ;) – Christian Rau Dec 04 '11 at 12:50

2 Answers2

6

A math coprocessor will be a great assistance with floating point operations but just about every modern CPU will have efficient integer division.

Seriously, just do the divide operation, it will almost certainly be the best option. While bit shifting is good for division by powers of two (like 1024), it's not so good otherwise.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

int always takes non-decimal answer.so use

int i=24378/1000;

24378/1000=24.378 but int will take 24

Its not blank
  • 3,055
  • 22
  • 37