A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
Arduino in Action
this week in the
General Computing
forum!
A special promo:
Enter your blog post or vote on a blogger to be featured in an upcoming Journal
JavaRanch
»
Java Forums
»
Java
»
Java in General
Author
fastest way to access certain digit in long
Hendra Kurniawan
Ranch Hand
Joined: Jan 31, 2011
Posts: 239
posted
Jun 01, 2012 22:32:57
0
lets say I have :
long number=1234567890;
and I want to get the fourth digit from behind (in this case, the number would be 7). What's the fastest way to get this number? Is there any faster way than this?
int digit=(number/1000)%10;
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2816
2
posted
Jun 01, 2012 22:44:39
0
I think that's the fastest way, right there.
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4901
7
I like...
posted
Jun 02, 2012 04:42:59
0
Hendra Kurniawan wrote:
Is there any faster way than this?
Not unless you want to get
really geeky
And I'd advise strongly against it.
Winston
Isn't it funny how there's always time and money enough to do it WRONG?
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
4
posted
Jun 02, 2012 05:59:05
0
It is much quicker if you want to access the
i
-th bit, or a digit in hexadecimal, which you can do with the << & and >> operators.
I agree. Here's the link:
http://zeroturnaround.com/jrebel
- it saves me about five hours per week
subject: fastest way to access certain digit in long
Similar Threads
Quickest way to read in a Web page
Sum Of Digits
Packed decimal conversion - Java
substring to just get the last character
multiples project (three five) project euler
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter