Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6641067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:46:01+00:00 2026-05-25T23:46:01+00:00

Imagine I have two byte[] arrays, b1 and b2, and they have the bytes

  • 0

Imagine I have two byte[] arrays, b1 and b2, and they have the bytes corresponding to two doubles.
One option would be something like…

double thisValue = readDouble(b1, s1);
double thatValue = readDouble(b2, s2);
return (thisValue < thatValue ? -1 : (thisValue == thatValue ? 0 : 1));

which uses…

/** Parse an integer from a byte array. */
public static int readInt(byte[] bytes, int start) {
  return (((bytes[start  ] & 0xff) << 24) +
          ((bytes[start+1] & 0xff) << 16) +
          ((bytes[start+2] & 0xff) <<  8) +
          ((bytes[start+3] & 0xff)));
}

/** Parse a long from a byte array. */
public static long readLong(byte[] bytes, int start) {
  return ((long)(readInt(bytes, start)) << 32) +
    (readInt(bytes, start+4) & 0xFFFFFFFFL);
}

/** Parse a double from a byte array. */
public static double readDouble(byte[] bytes, int start) {
  return Double.longBitsToDouble(readLong(bytes, start));
}

(code taken from apache hadoop source here and here).

The thing is, you have their byte representations, so it seems wasteful have to actually case them into a double, although maybe this is so heavily optimized as to be negligible. I am sure the Hadoop people know what they are doing, I’m just curious why it wouldn’t be better/faster to just compare the bits directly? Or maybe the compiler is smart enough to see this sort of thing and do just that.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:46:01+00:00Added an answer on May 25, 2026 at 11:46 pm

    Due to the structure of the IEEE floating-point format, you can’t simply check if all of the bits are identical: for example, -0 and +0 have distinct representations, but are considered equal; and NaN values, which have many different representations, are never equal to anything, including other NaN values with the same representation.

    While it is certainly possible to implement these checks yourself, it quickly becomes very complex, and not worth it: the “subvalues” you need to check do not have their own bytes, so you still have to extract the bytes and throw them into larger values – and then you have to actually check all of the different conditions.

    In other words, you end up doing the same things that the above piece of code is doing, but you’re spending many more lines of code, and you’re very unlikely to perform any better than what’s already there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's imagine that we have two arrays: $array_1 = array( '0' => 'zero', '1'
Imagine I have two tables in a DB like so: products: product_id name ----------------
I have two classes, basically one holds Members and the other Sessions. They are
Imagine you have two tables, with a one to many relationship. For this example,
Imagine I have two TextInput components. How do I know which one is active,
Imagine I have two class A and B defined like this : class A
I have two methods exactly like each other but just in a line imagine
Imagine you have two images A and B, and a third grayscale image T.
Imagine we have two tables in the database, user (FK id_role) and role (PK
Imagine I have two protocols: @protocol A @end and @protocol B <A> // Protocol

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.