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 145643

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:27:36+00:00 2026-05-11T08:27:36+00:00

byte x = -1; for(int i = 0; i < 8; i++) { x

  • 0
byte x = -1; for(int i = 0; i < 8; i++) {     x = (byte) (x >>> 1);     System.out.println('X: ' + x); } 

As I understand it, java stores data in two’s-complement, meaning -1 = 11111111 (according to wikipedia).

Also, from the java docs: ‘The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator ‘>>>’ shifts a zero into the leftmost position, while the leftmost position after ‘>>’ depends on sign extension. ‘

Which means that >>> would shift a 0 to the left most bit every time. So I expect this code to be

iteration: bit representation of x

0: 11111111

1: 01111111

2: 00111111

3: 00011111

…so on

However, my output is always X: -1, meaning (I guess) that >>> is putting the sign bit in the left most position. So I then try >>, and same result.

What’s going on? I would expect my output to be: X: -1, x: 127, x: 63, etc.

  • 0 0 Answers
  • 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. 2026-05-11T08:27:36+00:00Added an answer on May 11, 2026 at 8:27 am

    Whoever thought that bytes should be signed when Java was invented should be taken out and beaten with a wet stick of celery until they cry 🙂

    You can do what you want by casting up to an int and ensuring you never shift a 1 into the top bit, something like this:

    byte x = -1; int x2 = ((int)x) & 0xff; for(int i = 0; i < 8; i++) {     x2 = (x2 >>> 1);     System.out.println('X: ' + x2); } 

    Your particular problem is because >>> is casting up to an int to do the shift, then you’re casting it back to a byte, as shown here:

    byte x = -1; int x2 = ((int)x) & 0xff; int x3; int x4 = x2; for(int i = 0; i < 8; i++) {     x2 = (x2 >>> 1);     System.out.println('X2: ' + x2);     x3 = (x >>> 1);     x = (byte)x3;     x4 = (x4 >>> 1);     System.out.println('X: ' + x3 + ' ' + x + ' ' + x4); } 

    Which outputs:

    X2: 127 X: 2147483647 -1 127 X2: 63 X: 2147483647 -1 63 X2: 31 X: 2147483647 -1 31 X2: 15 X: 2147483647 -1 15 X2: 7 X: 2147483647 -1 7 X2: 3 X: 2147483647 -1 3 X2: 1 X: 2147483647 -1 1 X2: 0 X: 2147483647 -1 0 

    You can clearly see that x and x3 don’t work (even though x3 shifts correctly, casting it back to byte in x sets it to -1 again). x4 works perfectly.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Be sure to add the tabBar ABOVE your navigation bar… May 11, 2026 at 5:45 pm
  • Editorial Team
    Editorial Team added an answer This is dificult. Depending on what your client wants to… May 11, 2026 at 5:45 pm
  • Editorial Team
    Editorial Team added an answer I take it you can't just Insert into the existing… May 11, 2026 at 5:45 pm

Related Questions

Assumption: Converting a byte[] from Little Endian to Big Endian means inverting the order
Currently, I'm able to load in a static sized texture which I have created.
I don't know a better title, but I'll describe the problem. A piece of
I have to process a large byte array that is passed to my function.

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.