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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:19:57+00:00 2026-05-25T21:19:57+00:00

May be I am too tired. Why dont’t the following display the same value?

  • 0

May be I am too tired.
Why dont’t the following display the same value?

int x = 42405;
System.out.println(x << 8);  
System.out.println((x &0x00ff) << 8);  

The lower bits should be clear in both cases

  • 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-25T21:19:58+00:00Added an answer on May 25, 2026 at 9:19 pm

    EDIT: Okay, I’m leaving the bottom part for posterity…

    If x is int, then it’s pretty simple: x << 8 will have the same value as x & 0xff if and only if none of the “middle” 16 bits are set:

    • The top 8 bits of x will be rendered irrelevant by the left shift
    • The bottom 8 bits of x are preserved by the masking

    If there are any of the “middle” 16 bits set, then x & 0xff will differ from x in a bit which is still preserved by the shift, therefore the results will be different.

    I don’t understand why you’d expect them to always give the same results…


    I’m going to assume that the type of x is byte, and that it’s actually negative.

    There’s no shift operation defined for byte, so first there’s a transformation to int… and that’s what can change depending on whether or not you’ve got the mask.

    So let’s take the case where x is -1. Then (int) x is also -1 – i.e. the bit pattern is all 1s. Shift that left by 8 bits and you end up with a bit pattern of 24 1s followed by 8 0s:

    11111111111111111111111100000000 = -256
    

    Now consider the second line of code – that’s taking x & 0xff, which will only take the bottom 8 bits of the promoted value of x – i.e. 255. You shift that left by 8 bits and you end up with 16 0s, 8 1s, then 8 0s:

    00000000000000001111111100000000 = 65280
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

may be too simple groovy question but....please help i have a list like this:
This question may be too product specifc but I'd like to know if anyone
This question may sound too silly, however , I don't find concrete answer any
I think I have a synchronization problem...It may be too basic..Please help.. I have
Well, I'm learning Scala so this question may be too basic for most people.
i'm a long-time newbie to c#, and this question may be too obvious, so
Alright, I am going to state up front that this question may be too
OK, so this may be getting too complicated for CSS/HTML to handle, but I
this may sound too simple, but I'm missing something. I need to write a
May be my title is not clear. I am looking for some kind of

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.