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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:31:58+00:00 2026-06-11T23:31:58+00:00

Consider the following code (where byteIndex is an int): int bitNumber = b-(8*byteIndex); bitMask

  • 0

Consider the following code (where byteIndex is an int):

int bitNumber = b-(8*byteIndex);
bitMask = 0x8>>(byte)bitNumber;

This generates the error

error: possible loss of precision

when compiled (required byte, found int).

The code

int bitNumber = b-(8*byteIndex);
bitMask = 0x8>>2;

compiles fine.

What is the problem here and how do I fix the first example to allow bit shifting by the int value?

EDIT: Following the comments, here is a more-complete example:

48) int byteIndex;
49) byte bitMask;
50) int bitNumber;
    // assign value to byteIndex
67) bitNumber = b-(8*byteIndex);
68) bitMask = 0x8>>bitNumber;

and the error given is:

...MyClass.java:68: error: possible loss of precision
    bitMask = 0x8>>bitNumber;
             ^
  required: byte
  found:    int
1 error
  • 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-06-11T23:32:00+00:00Added an answer on June 11, 2026 at 11:32 pm

    Convert your shifting line to this: –

    byte bitMask = (byte)(0x8>>(byte)bitNumber);
    

    Your RHS, is an int, you need to typecast it to byte..

    Above code will work fine.. With or without the casting of bitNumber to byte

    So, you can also have : –

    byte bitMask = (byte)(0x8>>bitNumber);
    

    But, here’s is a question – byte bitMask = 0x8>>3; works fine.. Why is it so??

    Here’s some example to explain the reason behind its working and also the behaviour with final: –

    byte bitMask;
    int varInt1 = 3;
    final int finalVarInt2 = 3;
    final int finalVarInt3 = 4;
    
    bitMask = 0x8>>varInt1;    // 1. Will not work. 
    bitMask = 0x8<<3;          // 2. Will work
    
    bitMask = 0x8<<4;          // 3. Will not work
    bitMask = 0x8<<finalVarInt2;   // 1. Will work
    bitMask = 0x8<<finalVarInt3;   // 2. Will not work
    

    Here’s some reasoning that explains the above behaviour: –

    • The value on the RHS will be typecasted implicitly only if, the compiler is sure that, it will be able to accomodate that value in the byte variable on LHS.. Else, we have to do Explicit type casting to tell compiler that, we know what we are doing, just do it for us..

    Now lets consider all the cases one-by-one (From the above code (1-3, 1-2): –

    1. varInt1 initially contains 3. So the value of RHS evaluates to 64. Although this value might get accomodated to byte variable in LHS, but compiler also knows that, it is possible to change the value of varInt1.. So what if value of varInt1 is changed to 4 at some stage.. It won’t work then.. That’s why it is not allowed..
    2. Now, in this case, since we have explicitly used an Integer Literal here, so compiler is sure that it will accomodate in byte.. So it allows the implicit casting..
    3. Again, in this case, it is known that RHS will evaluate to 128 which can’t be accomodated in byte.. Failed again..

    Last two cases are different from regular variables… Since they are declared final, they can’t be re-initialized.. So, compiler can make a decision based on the assigned value..

    1. In this case, compiler sees that, finalVarInt2 contains value 3. So, RHS evaluates to 64, which can be accommodated in the byte variable on LHS. Now, since the variable is final it can’t be changed, and Compiler knows that, so it is sure that t*his value will always be 64*.. So compiler allows this.

    2. In the last case, value of finalVarInt3 is 4.. Similar reasoning.. Won’t fit in LHS, as RHS evaluates to 128 which can’t fit into byte

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

Sidebar

Related Questions

Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
Consider the following code in C: void main() { int a=0; for(printf(\nA); a; printf(\nB));
Consider the following code example: TempList.ForEach(Function(obj) obj.Deleted = True End Function) And this one:
Consider the following code: function Robot(weapon) { this.weapon = weapon; this.fire = function() {
Consider the following code: #include <iostream> using namespace std; class Test { static int
consider the following code addHash(hash); bool addHash(char* hash) { HKEY hKey = 0; int
Consider the following code: $('div').click(function(){ $(this).animate({height:100}, 500) $(this).css({opacity:1}); }); Versus: $('div').click(function(){ $(this).animate({height:100}, 500); })
This is rather interesting, I think. Consider following code, both the window.onload and body
Consider the following code: function authenticate(){ $this->session->set_userdata('logged_in', true); } How do I return this
consider the following code: public class SynchronizedCounter extends Thread { private int c =

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.