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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:28:00+00:00 2026-06-12T20:28:00+00:00

This is a really basic question, but I’ve never fully convinced myself that my

  • 0

This is a really basic question, but I’ve never fully convinced myself that my intuitive answer of “it makes no difference” is correct, so maybe someone has a good way to understand this:

If all I want to do with one of the primitive numeric types in Java is bitwise arithmetic, can I simply treat it as if it was an unsigned value or do I need to avoid negative numbers, i.e. keep the highest order bit always set to 0? For example, can I use an int as if it was an unsigned 32-bit number, or should I only use the lowest 31 bits?

I’m looking for as general an answer as possible, but let me give an example: Let’s say I want to store 32 flags. Can I store all of them in a single int, if I use something like

store = store & ~(1 << index) | (value << index)

to set flag index to value and something like

return (store & (1 << index)) != 0

to retrieve flag index? Or could I run into any sort of issues with this or similar code if I ever set the flag with index 31 to 1?

I know I need to always be using >>> instead of >> for right shifting, but is this the only concern? Or could there be other things going wrong related to the two’s complement representation of negative numbers when I use the highest bit?

  • 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-06-12T20:28:02+00:00Added an answer on June 12, 2026 at 8:28 pm

    There are legitimate reasons to use >> as well in that context (a common case is when making a mask that should be 0 or -1 directly, without having to negate a mask that is 0 or 1), so there is really no concern at all. Just be careful of what you’re doing to make sure it matches your intent.

    Operations that care about signedness (ie they have distinct signed and unsigned forms with different semantics) are:

    • right shift
    • division (unsigned form not available in Java)
    • modulo (unsigned form not available in Java)
    • comparisons (except equality) (unsigned forms not available in Java)

    Operations that don’t care about signedness are:

    • and
    • or
    • xor
    • addition
    • subtraction
    • two’s complement negation (-x means ~x + 1)
    • one’s complement (~x means -x - 1)
    • left shift
    • multiplication
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a really basic syntax question, but I can't find the answer.
This is a really basic question but I couldn't find an answer anywhere... How
This question seems really basic, but I cant find the answer by google, and
I'm pretty sure that this is a basic question but I really don't know
Sorry if this is a really basic question but it's been really getting to
I know this is a really basic question but I've been searching the internet
This feels like a really basic question, but I can't figure it out anyway..
I realize this question is pretty basic, but I'm really stuck. I have a
This question is really basic. What is the performance difference between removing a UIView
This is a really basic regex question but since I can't seem to figure

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.