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 4022408
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:28:16+00:00 2026-05-20T10:28:16+00:00

I have a feeling this is a rather trivial question, but I’m stumped. In

  • 0

I have a feeling this is a rather trivial question, but I’m stumped. In my application I’m keying things in a lookup table with a pair of ints. I thought it would be easier to concatenate the two ints into one long and use the single long as a key instead. Coming from a C background, I was hoping something like this would work:

int a, b;
long l = (long)a << 32 | b;

My attempts to replicate this in Java have frustrated me. In particular, because there are no unsigned integral types, I can’t seem to avoid the automatic sign-extension of b (a gets left-shifted so its irrelevant). I’ve tried using b & 0x00000000FFFFFFFF but it surprisingly has no effect. I also tried the rather ugly (long)b << 32 >> 32, but it seemed to be optimized out by the compiler.

I was hoping to do this strictly using bit manipulation with primitives, but I’m starting to wonder if I need to use some sort of buffer object to achieve this.

  • 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-20T10:28:17+00:00Added an answer on May 20, 2026 at 10:28 am

    I always use my utility class with

    public static long compose(int hi, int lo) {
        return (((long) hi << 32) + unsigned(lo));
    }
    public static long unsigned(int x) {
        return x & 0xFFFFFFFFL;
    }
    
    public static int high(long x) {
        return (int) (x>>32);
    }
    public static int low(long x) {
        return (int) x;
    }
    

    For any int x, y (negative or not)

    high(compose(x, y)) == x
    low(compose(x, y)) == y
    

    holds and for any long z

    compose(high(z), low(z)) == z
    

    holds, too.

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

Sidebar

Related Questions

I have a feeling this is a stupid question but I can't find the
I have the feeling the answer to this question is no but I figured
I have a feeling that this is stupid question, but I'll ask anyway... I
I have a feeling I'm going about this all wrong. But anyway. I have
I have a feeling I am completely borking this MySQL query but I'll ask
This doesn't make sense to me, but I have a feeling I saw a
Keep going around circles, but I am still unclear about this. Have a feeling
I have a feeling this XML is not valid, can someone please explain why?
I have a feeling that this query is pretty easy to construct, I just
I'm maintaining a CMS where I have this feeling it was made in the

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.