• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

re:Shift Operators

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi every one. This is my first post here. I am preparing for the SCJP, and am facing difficulty in solving the shift operators problems. I request that any good links on this subject be posted, which explains how to solve them.
Thanks.
Vijay Naidu
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I don't know of any links in particular, but the book that helped me the most with that subject is "A Programmer's Guide to Java Certification" by Mughal and Rasmussen. (pp. 68-71)
 
Vijay Naidu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Peter, only problem is I dont have that book!
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vijay,
I wrote the following code. Play around with it. It cleared my doubts.

// Vaneet Bhutani
// Prog Number: 1.
// Module: Shift Operators 101
// Expected output: 1 unsigned right shift:2147483519
// i:-257
// 2 signed right shift:-65
// i:-257
// 3 signed left shift:-2056
// i:-257

import java.lang.*;
public class ShiftTest
{
public static void main(String args[])
{
int i =-257; //try i=257
int j;
j=i>>>1;
//Remember the brackets. Otherwise, you will recv. Invalid arguments to >> error message.
System.out.println("1 unsigned right shift:" + j);
System.out.println("i:"+i);
System.out.println("2 signed right shift:" + (i>>2));
System.out.println("i:" + i);
System.out.println("3 signed left shift:" + (i<<3));
System.out.println("i:"+i);
}
}

Originally posted by Vijay Naidu:
Hi every one. This is my first post here. I am preparing for the SCJP, and am facing difficulty in solving the shift operators problems. I request that any good links on this subject be posted, which explains how to solve them.
Thanks.
Vijay Naidu


 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) The shift operators can only be performed on int's or longs.
2) All dropped bits do nothing.
3) Left-shift '<<' and unsigned right-shift '>>>' replace bits that have been shifted with 0.
4) Signed right-shift >> takes value 0f most significant bit before the shift.
Examples of shifts:
15>>2 15 to binary is (More 0's then represented for int)
00001111 becomes 00000011 or int of 3.
15<<2
00001111 becomes 00111100 or int of 60.
Hope this can start you out in the right direction of the basics of the shift operators...
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One note on Jeff's comment. A shift can actullay be applied to a byte also, but when it happens, it changes the byte to an int first and then does the shift. It fills the other bits with the most significant value so for positve numbers it will add 24 0's and for negative numbers it will add 24 1's. After the shift, the number is still an int and you would cast it back to a byte. This can cause problems when you have a negative number and you have un-signed shift right (>>>) because it fills the first digits of the int with 0's and not the first digits of the byte. Probably easier to understand with an example.
-64>>>4 where -64 is in byte format equal to 11000000.
The first thing that happens is that Java promotes this to an int so you get 11111111 11111111 11111111 11000000
Then you apply the unsigned right shift operater and you get
00001111 11111111 11111111 11111100.
Then if you cast this back to a byte agin you get: 11111100 when you probably expected to get 00001100.
So when Jeff said a shift only applies to int and longs he is right, but the compiler won't complain if you try to use a byte also. But it should be avoided because of the situation above will give you unexpeted answers.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vijay,
Here's a link to some notes I've made on shift operators.
Easiest way to handle them is by remembering the formulas:
Left-shift << is same as (x * 2<sup>n</sup>)<br /> Right-shift >> is same as (x / 2n)
Hope that helps.
(10/07 - sorry, corrected the link)
------------------
Jane
[This message has been edited by Jane Griscti (edited October 05, 2000).]
[This message has been edited by Jane Griscti (edited October 07, 2000).]
 
Vijay Naidu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, folks!, many a thanx for all the responses. I really appreciate your help . This will defenitely help sharpen the area where I was facing difficulty.
Thanks again.
Vijay Naidu
 
Vijay Naidu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jane,
I tried getting to the link that you had given for operators, but the page cannot be found.
Thanks.
Vijay Naidu
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just remove the target = _blank from his link . u will get the page
http://webhome.idirect.com/~jgriscti/oper/shift.html
[This message has been edited by Anbooo Sanygao (edited October 05, 2000).]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found www.jchq.net/tutorial/BitShift.htm helpful.
Marilyn
 
You firghten me terribly. I would like to go home now. Here, take this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic