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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:44:47+00:00 2026-05-11T16:44:47+00:00

int i =132; byte b =(byte)i; System.out.println(b); Mindboggling. Why is the output -124 ?

  • 0
int i =132;

byte b =(byte)i; System.out.println(b);

Mindboggling. Why is the output -124?

  • 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-11T16:44:48+00:00Added an answer on May 11, 2026 at 4:44 pm

    In Java, an int is 32 bits. A byte is 8 bits .

    Most primitive types in Java are signed, and byte, short, int, and long are encoded in two’s complement. (The char type is unsigned, and the concept of a sign is not applicable to boolean.)

    In this number scheme the most significant bit specifies the sign of the number. If more bits are needed, the most significant bit (“MSB”) is simply copied to the new MSB.

    So if you have byte 255: 11111111
    and you want to represent it as an int (32 bits) you simply copy the 1 to the left 24 times.

    Now, one way to read a negative two’s complement number is to start with the least significant bit, move left until you find the first 1, then invert every bit afterwards. The resulting number is the positive version of that number

    For example: 11111111 goes to 00000001 = -1. This is what Java will display as the value.

    What you probably want to do is know the unsigned value of the byte.

    You can accomplish this with a bitmask that deletes everything but the least significant 8 bits. (0xff)

    So:

    byte signedByte = -1;
    int unsignedByte = signedByte & (0xff);
    
    System.out.println("Signed: " + signedByte + " Unsigned: " + unsignedByte);
    

    Would print out: "Signed: -1 Unsigned: 255"

    What’s actually happening here?

    We are using bitwise AND to mask all of the extraneous sign bits (the 1’s to the left of the least significant 8 bits.)
    When an int is converted into a byte, Java chops-off the left-most 24 bits

    1111111111111111111111111010101
    &
    0000000000000000000000001111111
    =
    0000000000000000000000001010101
    

    Since the 32nd bit is now the sign bit instead of the 8th bit (and we set the sign bit to 0 which is positive), the original 8 bits from the byte are read by Java as a positive value.

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

Sidebar

Ask A Question

Stats

  • Questions 305k
  • Answers 305k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Writing the question oftens results in realising the answer. targetxspeed… May 13, 2026 at 8:59 pm
  • Editorial Team
    Editorial Team added an answer With regex "b sampletext3".replace(/^b/gm,function(x){return x.toUpperCase()}) B sampletext3 And assigning it… May 13, 2026 at 8:59 pm
  • Editorial Team
    Editorial Team added an answer Use the Image#to_blob method to turn the in-memory image into… May 13, 2026 at 8:59 pm

Related Questions

int i = 0; int min = x[i]; while ( i < n ){
for (int i = 0 ; i < stlVector.size() ; i++) { if (i
Given: int i = 42; int j = 43; int k = 44; By
int main() { int i,j; for (i=1; i<=25; i++) { for (j=2; j<= i/2;

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.