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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:19:28+00:00 2026-05-28T11:19:28+00:00

I am building a lightweight high-precision arithmetic library for one of my Java applications

  • 0

I am building a lightweight high-precision arithmetic library for one of my Java applications and have been doing quite a bit of bit-twiddling (pun intended). Occasionally I get an int that has to be be converted into a long via simple word extension i.e. the 32 least significant bits of the long should become equal to the bits of the int.

Unfortunately, casting only works correctly for positive numbers. In addition, using an int in the same arithmetic expression with a long will implicitly cast to long beforehand, which means that e.g. this will not work if i is negative:

long l = (i & 0xffffffffL);

Currently I am using something along the lines of:

long l = (((long)(i >>> 8)) << 8) | (i & 0xff);

Is there a more elegant way to do this?

EDIT:

I may be missing something:

int i = -1;

long l = (i | 0xffffffffL);

System.out.println(l);

This prints out -1 rather than 4294967295. What am I missing?

EDIT 2:

Ooops… | instead of &. How the **** did the little ****** get there?

EDIT 3:

I have no idea how exactly I missed it, but this works perfectly:

long l = (i & 0xffffffffL);

…which makes this question completely irrelevant, I guess.

  • 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-28T11:19:29+00:00Added an answer on May 28, 2026 at 11:19 am

    Did you actually test the long l = (i & 0xffffffffL); code? Yes, the value of i will be converted to long first, which means either 0-extension if i is positive or 1-extension if i is negative. In either case the lower 32 bits are not affected, so unless I misunderstand the problem, the & operation should leave you with the result you want.

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

Sidebar

Related Questions

I have been building applications / web apps for some time, and I guess
Building on what has been written in SO question Best Singleton Implementation In Java
I have been tasked with building a simple web based system for managing a
Building my first SL MVVM application (Silverlight4 RC) and have some issues i don't
Building a commercial product may use various open source libraries that have use of
I'm building Rails application and I want to have user registration/login functionality. I also
building on my last question jQuery getting values from multiple selects together I have
Building a rails B2B application that will have various users. I'm pretty clear on
I have been using PHP for years. Lately I've come across numerous forum posts
I am building a news app (TabBar app) where I have to load top

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.