logo elektroda
logo elektroda
X
logo elektroda

JAVA - Bit Shifts in Java: Right, Left & Right Zeros with int Variable of Value 256

novix8 13398 6
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 12823510
    novix8
    Level 10  
    Hello everyone ;)
    I learn java on my own from the book "Practical JAVA course". My problem is that I stopped on a task which reads "Declare an int variable of any name. Assign it the value 256. Do three things: bit shift right two places, bit shift left two places, and bit shift to the right two places with zeros. Display the result of all three actions on the screen. "
    I managed to create something like this:
    Code: Java
    Log in, to see the code

    The only problem is that somewhere I should change my variable to a binary system, but I don't really know how; / Could someone help / direct?
  • ADVERTISEMENT
  • #2 12824069
    Anonymous
    Anonymous  
  • ADVERTISEMENT
  • #3 12824832
    novix8
    Level 10  
    Thanks for the hint ;) But to be honest, I've been working on it for a while and I'm still not doing it; /

    Code: Java
    Log in, to see the code


    Yes, I changed the program, but I think something is wrong: / I can't use If's yet, because I haven't figured it out yet. I have to show it the easiest way.
  • ADVERTISEMENT
  • #4 12825034
    Anonymous
    Anonymous  
  • #5 12825153
    novix8
    Level 10  
    Thanks for your patience ;) And I have another question ;) Why was the second variable (b) needed, and why a = 255 and not 256 and b = 2?
  • ADVERTISEMENT
  • #6 12825480
    Anonymous
    Anonymous  
  • #7 12852840
    memlon
    Level 11  
    This course helped me a lot in learning this language Link .

Topic summary

The discussion revolves around performing bit shifts in Java using an integer variable initialized to 256. The user seeks assistance in correctly implementing right shift (>>), left shift (>) operations, as well as converting the integer to its binary representation. Responses provide guidance on using the `Integer.toBinaryString()` method for binary conversion and clarify the significance of the second variable in the bit shift operations. The importance of understanding the limitations of shifting values, particularly with the integer 256, is emphasized, along with examples demonstrating the results of the bit shift operations.
Summary generated by the language model.
ADVERTISEMENT