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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:31:20+00:00 2026-05-26T23:31:20+00:00

I am having trouble understanding converting to and from bytes, or bytes in general

  • 0

I am having trouble understanding converting to and from bytes, or bytes in general

byte b = (byte)170;
System.out.println("Byte to int" + (int)b);

why is it that when I cast 170 to a byte then cast that byte to an int I get -86 and what is the proper way to do this?

EDIT: Okay so the answer to my question was really simple (byte ranges from -128 to 127)…
Where I was getting confused is that I really want is binary 10101010 to be in the byte so I figured represent that number in int and put that in the byte. Can someone please tell me how I can get that binary number into a byte?

  • 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-26T23:31:21+00:00Added an answer on May 26, 2026 at 11:31 pm

    The binary representation of 170 as a 32-bit integer is

    0000 0000 0000 0000 0000 0000 1010 1010 
    

    When you cast that to a byte, it becomes 1010 1010. Since all integers in Java are signed, it sees 1010 1010 as a 2’s complement 8-bit integer. That’s -86.

    When you convert this byte back to integer using (int) b, it gets signed extended to:

    1111 1111 1111 1111 1111 1111 1010 1010 
    

    Which is also -86. You want to apply a bit mask to it, so it becomes 170 again.

    (int) (0xff & b) will give you 170 back:

    1111 1111 1111 1111 1111 1111 1010 1010 
    0000 0000 0000 0000 0000 0000 1111 1111 & (bitwise and)
    ---------------------------------------
    0000 0000 0000 0000 0000 0000 1010 1010 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

from here Im having trouble understanding they say that ado.net does not include a
I am having trouble understanding how the System Registry can help me convert a
I'm having trouble understanding space complexity. My general question is: how can the space
I am having a little trouble understanding ServiceKnownType in WCF. Taken from this blog
I'm having trouble understanding the result of the following statements: >>> from array import
I am having trouble understanding how escaping works inside html tag attribute values that
I am having trouble understanding the difference between these two commands that in my
I'm having trouble understanding the following bit of code that I was hoping would
I'm having trouble understanding why Python raises a TypeError when you provide arguments that
I'm having trouble understanding following code: if (IsPostBack) { int CheckBoxIndex; ArrayList CheckBoxArray; bool

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.