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

  • Home
  • SEARCH
  • 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 986245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:17:27+00:00 2026-05-16T05:17:27+00:00

I want to understand how to convert signed number into unsigned. lets say I

  • 0

I want to understand how to convert signed number into unsigned.

lets say I have this:

byte number = 127; // '1111111'

In order to make it unsigned I have to choose “bigger” data type ‘short’ and apply AND operator with value 0x00ff.

short number2;  
number2 = number & 0x00ff;

Why does it make the number unsigned?

  • 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-16T05:17:27+00:00Added an answer on May 16, 2026 at 5:17 am

    Java doesn’t actually have unsigned primitives.

    The value 127 is actually represented by ‘01111111’ the first bit being the sign (0 is positive).

    An unsigned byte would be able to hold values 0 to 255, but 127 is the maximum for a signed byte. Since a byte has 8 bits, and the signed one consumes one to hold the sign.
    So if you want to represent values larger than 127 you need to use a bigger type that has a greater number of bits. The greater type also has a reserved bit for sign, but it has at least 8 bits used for the actual values, so you can represent the value 255.

    That being said, you should probably avoid using byte and short because there are issues with them. You’ll notice i cast the result to short, since the operators actually return int. You should just stick to int and long in java since they are implemented better.

    Edit: the AND operator makes it unsigned since the sign bit is the first bit of the short, and you copy the 8 bits holding the value of the byte to the last 8 bits of the short. So if you have a negative number the first bit which is 1 (that means it’s negative) actually becomes part of the value. And the short will always be positive since its sign bit is at two high of a power of two to be affected by the short.

     byte:             10101101
                        ||||||| <- actual value
    short:     0000000010101101
                ||||||||||||||| <- actual value
    

    Edit 2: Take note though that since the negative values use two’s complement representation the value might not be what you expect it. all the positive values remain the same.
    But -128 = 0x10000000 will become 128
    -127 = 0x10000001 will become 129
    and so on until -1 = 0x11111111 which will become 255

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

Sidebar

Related Questions

This is probably an easy question, but I want to understand better how Apache
I am developing a software that uses number precision, but I have this problem,
I'm reading Paul Graham's A Plan for Spam and want to understand it better
As far as I understand, if I want to get my database under source
Yes, I do understand the difference between them. What I want to know is:
I want to assign the decimal variable "trans" to the double variable "this.Opacity". decimal
Want to know what the stackoverflow community feels about the various free and non-free
Want my FireFox at work to be in sync with my FireFox at my
I want to loop over the contents of a text file and do a
I want to create a Java application bundle for Mac without using Mac. According

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.