This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Shifting Rules?? (+/-SRS,+/-LS+/-URS) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Shifting Rules?? (+/-SRS,+/-LS+/-URS)" Watch "Shifting Rules?? (+/-SRS,+/-LS+/-URS)" New topic
Author

Shifting Rules?? (+/-SRS,+/-LS+/-URS)

Vidya Selvaraj
Greenhorn

Joined: Nov 21, 2000
Posts: 29
1) + >>, + >>>
+ N >> S, + N >>> S
For all values of N & S, can I use
N / (2 ^ (s%32) )
eg.
32 >> 7 = 32/ 2 ^ 7 = 1/4 = 0
32 >> 34 = 32 / 2 ^ 2 = 8
11 >>> 4 = 11/16 = 0
2) + <<, - <<<br /> + N << S , - N << S<br /> For all values of N & S, can I use <br /> N * (2 ^ (s%32) )<br /> eg. <br /> 32 << 7 = 32 * 2 ^ 7 = 4096<br /> 32 << 34 = 32 * 2 ^ 2 = 128<br /> 5 << 2 = 5 * 4 = 20<br /> - 5 << 2 = - ( 5 * 4) = 20<br /> - 32 << 7 = - ( 32 * 2 ^ 7) = -4096<br /> - 32 << 34 = - ( 32 * 4 ) = 128<br /> 3 ) - >>><br /> - N >>> S<br /> For all values of N & S, can I use<br /> (2 ^ 32) - 1 / ( 2 ^ ( s % 32))<br /> For eg<br /> -32 >>> 34 = 4294967296 / 4 = 10737419...<br /> - 6 >>> 3 = 4294977296 / 8 = 536870....<br /> 4 ) - >><br /> - N >> S<br /> This is where I am really confused. I am not able to follow one solid rule<br /> I have been following this logic, but it doesn't work for certain numbers, why?? I got this logic from from one of the posting in this site<br /> a) - 16 >> 2<br /> 16 : 0000 0000 0000 0000 0000 0000 0001 0000<br /> ~ : 1111 1111 1111 1111 1111 1111 1110 1111<br /> >> 2 : 1111 1111 1111 1111 1111 1111 1111 1011
~ : 0000 0000 0000 0000 0000 0000 0000 0100
= - ( 4)
b) - 8 >> 2
8 : 0000 0000 0000 0000 0000 0000 0000 1000
~ : 1111 1111 1111 1111 1111 1111 1111 0111
>> 2 : 1111 1111 1111 1111 1111 1111 1111 1101
~ : 0000 0000 0000 0000 0000 0000 0000 0010
= - ( 2)
BUT the foll doesn't work, why??
c) - 5 >> 2
5 : 0000 0000 0000 0000 0000 0000 0000 0101
~ : 1111 1111 1111 1111 1111 1111 1111 1010
>> 2 : 1111 1111 1111 1111 1111 1111 1111 1110
~ : 0000 0000 0000 0000 0000 0000 0000 0001
= - ( 1) , but the correct answer is -2
d) -32 >> 34 gives 0, but the correct answer is -8
Thanx
Vidya

Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
Hi Vidya,
I'll talk about your problem shifitng negative numbers. Basically the way you got the 2's Complemnt number for a -ve number is wrong. Rule is
1. Take binary representation for abs value of the -ve number
2. Take the complement of that number.
3. add the 1 to the above result.
Now you have the binary representation for the -ve number.
For Ex: -16
1. 0000 0000 0000 0000 0000 0000 0001 0000
2. 1111 1111 1111 1111 1111 1111 1110 1111
3. 1111 1111 1111 1111 1111 1111 1111 0000
Now Shift whatever number you want. In your case, 2
1111 1111 1111 1111 1111 1111 1111 1100
Because this is the -ve number (hight bit 1), you have to follow the above shown procedure
2. 0000 0000 0000 0000 0000 0000 0000 0011
3. 0000 0000 0000 0000 0000 0000 0000 0100
This is -4. Now let's do for -5
1. 0000 0000 0000 0000 0000 0000 0000 0101
2. 1111 1111 1111 1111 1111 1111 1111 1010
3. 1111 1111 1111 1111 1111 1111 1111 1011
Now shift 2.
1111 1111 1111 1111 1111 1111 1111 1110
2. 0000 0000 0000 0000 0000 0000 0000 0001
3. 0000 0000 0000 0000 0000 0000 0000 0010
This is -2. I hope you got this.
Santhosh.
Barbara Foute Nelong
Greenhorn

Joined: Nov 30, 2000
Posts: 20
what you mean by "add the 1 to the above result" in rule 3?
Thanks...I am confused
Vidya Selvaraj
Greenhorn

Joined: Nov 21, 2000
Posts: 29
Santhosh, I tried for -32 >> 34 (i.e., -32 >> 2), its not working, can you help me with this??
(32 ): 0000 0000 0000 0000 0000 0000 0001 0000
(~ ): 1111 1111 1111 1111 1111 1111 1110 1111
(+ 1 ): 1111 1111 1111 1111 1111 1111 1111 0000
(>> 2): 1111 1111 1111 1111 1111 1111 1111 1100
(~ ): 0000 0000 0000 0000 0000 0000 0000 0011
(+ 1 ): 0000 0000 0000 0000 0000 0000 0000 0100
= - 4 ??? Where am I wrong??
Thanx
Vidya
rajani peddi
Ranch Hand

Joined: Nov 27, 2000
Posts: 73
vidya,
adding 1 means adding a '1' to the binary number:

1+1==>0 and a carry of 1
1+0==>1 no carry

Example:
0100111
0000001
-------
0101000

Hope this clears...
rajani

[This message has been edited by rajani peddi (edited December 01, 2000).]
rajani peddi
Ranch Hand

Joined: Nov 27, 2000
Posts: 73
Originally posted by Vidya Selvaraj:
Santhosh, I tried for -32 >> 34 (i.e., -32 >> 2), its not working, can you help me with this??
(32 ): 0000 0000 0000 0000 0000 0000 0001 0000
(~ ): 1111 1111 1111 1111 1111 1111 1110 1111
(+ 1 ): 1111 1111 1111 1111 1111 1111 1111 0000
(>> 2): 1111 1111 1111 1111 1111 1111 1111 1100
(~ ): 0000 0000 0000 0000 0000 0000 0000 0011
(+ 1 ): 0000 0000 0000 0000 0000 0000 0000 0100
= - 4 ??? Where am I wrong??
Thanx
Vidya


hey vidya,

32 in binary is
0000 0000 0000 0000 0000 0000 0010 0000
thats where u went wrong
rajani

[This message has been edited by rajani peddi (edited December 01, 2000).]
Vidya Selvaraj
Greenhorn

Joined: Nov 21, 2000
Posts: 29
Hi Rajani, I do not have problems with adding + 1 (binary add). When I apply the logic as stated by Santhosh, it is not working for -32 >> 34. Could you help me with this??
Thanx
Vidya
Vidya Selvaraj
Greenhorn

Joined: Nov 21, 2000
Posts: 29
Oops, Thanks Rajani for pointing me that out!
Regards
Vidya
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
Hi Vidya and Rajani,
I have written a smalll program to solve the problem. Pl go thru this. It will give the step by step binary values. I hope this will solve your doubts.
<code>
public class ShiftTest {
public static void main(String[] args) {

int toBeShifted = 32;
int shiftBy = 34;
System.out.println("Binary representation for " + toBeShifted + " >> " + shiftBy + ":");
System.out.println("Binary for " + Math.abs(toBeShifted) + ": " + append(Math.abs(toBeShifted)));
System.out.println("Complement of above: " + append(~Math.abs(toBeShifted)));
System.out.println("Add 1: " + append((~Math.abs(toBeShifted)) + 1));
System.out.println("Shift by 34(" + 34%32 + "): " + append(((~Math.abs(toBeShifted)) + 1)>> shiftBy));
System.out.println("Complement: " + append(~(((~Math.abs(toBeShifted)) + 1)>> shiftBy)));
System.out.println("Add 1: " + append((~(((~Math.abs(toBeShifted)) + 1)>> shiftBy)) + 1));
System.out.println("=: " + (toBeShifted >> shiftBy)) ;

}

static String append(int no) {

String str = "";
int length = (Integer.toBinaryString(no)).length();
for( int i = 0; i < 32 - length ; i++) {

str += "0";
}
str += Integer.toBinaryString(no);
return str;
}

}
</code>
Vidya Selvaraj
Greenhorn

Joined: Nov 21, 2000
Posts: 29
Thanks a lott Santhosh for your patience.
Regards
Vidya
Dipali Desai
Greenhorn

Joined: Oct 07, 2000
Posts: 14
hi vidya,
please i did not follow ur 3rd point that is -N >> S can u be more specific please .
the rest of the Explanation was Excellent. there couln't have been a better Explanation .Thanx to Santosh too.
bye
dipali
Originally posted by Vidya Selvaraj:
1) + >>, + >>>
+ N >> S, + N >>> S
For all values of N & S, can I use
N / (2 ^ (s%32) )
eg.
32 >> 7 = 32/ 2 ^ 7 = 1/4 = 0
32 >> 34 = 32 / 2 ^ 2 = 8
11 >>> 4 = 11/16 = 0
2) + <<, - <<<br /> + N << S , - N << S<br /> For all values of N & S, can I use <br /> N * (2 ^ (s%32) )<br /> eg. <br /> 32 << 7 = 32 * 2 ^ 7 = 4096<br /> 32 << 34 = 32 * 2 ^ 2 = 128<br /> 5 << 2 = 5 * 4 = 20<br /> - 5 << 2 = - ( 5 * 4) = 20<br /> - 32 << 7 = - ( 32 * 2 ^ 7) = -4096<br /> - 32 << 34 = - ( 32 * 4 ) = 128<br /> 3 ) - >>><br /> - N >>> S<br /> For all values of N & S, can I use<br /> (2 ^ 32) - 1 / ( 2 ^ ( s % 32))<br /> For eg<br /> -32 >>> 34 = 4294967296 / 4 = 10737419...<br /> - 6 >>> 3 = 4294977296 / 8 = 536870....<br /> 4 ) - >><br /> - N >> S<br /> This is where I am really confused. I am not able to follow one solid rule<br /> I have been following this logic, but it doesn't work for certain numbers, why?? I got this logic from from one of the posting in this site<br /> a) - 16 >> 2<br /> 16 : 0000 0000 0000 0000 0000 0000 0001 0000<br /> ~ : 1111 1111 1111 1111 1111 1111 1110 1111<br /> >> 2 : 1111 1111 1111 1111 1111 1111 1111 1011
~ : 0000 0000 0000 0000 0000 0000 0000 0100
= - ( 4)
b) - 8 >> 2
8 : 0000 0000 0000 0000 0000 0000 0000 1000
~ : 1111 1111 1111 1111 1111 1111 1111 0111
>> 2 : 1111 1111 1111 1111 1111 1111 1111 1101
~ : 0000 0000 0000 0000 0000 0000 0000 0010
= - ( 2)
BUT the foll doesn't work, why??
c) - 5 >> 2
5 : 0000 0000 0000 0000 0000 0000 0000 0101
~ : 1111 1111 1111 1111 1111 1111 1111 1010
>> 2 : 1111 1111 1111 1111 1111 1111 1111 1110
~ : 0000 0000 0000 0000 0000 0000 0000 0001
= - ( 1) , but the correct answer is -2
d) -32 >> 34 gives 0, but the correct answer is -8
Thanx
Vidya

Vidya Selvaraj
Greenhorn

Joined: Nov 21, 2000
Posts: 29
Hi Dipali,
I am not sure whether you are asking about -N >>> S or -N >> S. -N >> S has a clear explanation given by Santhosh & Rajani.

Remember if S = 32 or 64 for int or long,
whatever you do for +/- N >>, <<, >>> S, the shift will not be performed, For eg: -5 >>> 32 = -5; -6 << 32 = -6; 6 >> 32 = 6
Michael Burke
Ranch Hand

Joined: Jul 29, 2000
Posts: 103
Santosh, thanks for the code example, too bad Sun doesn't provide a toBinaryString() method that works with negative numbers.
Shan Karawita
Ranch Hand

Joined: Oct 13, 2000
Posts: 83

Wow ! The Code example is really great !

Well Done Santosh !
It'll really helpful for aspirants like us .
Sahir Shibley
Ranch Hand

Joined: Apr 08, 2000
Posts: 275

Good lord. What is all this ?
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
hi Michael Burke,
toBinaryString works with -ve numbers too. But I wanted to show how to do manually and compare with the code result. So I had written like that.
Santhosh.
Michael Burke
Ranch Hand

Joined: Jul 29, 2000
Posts: 103
According to the API I thought it only works with pos no. I'll have to try it with a neg. no.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shifting Rules?? (+/-SRS,+/-LS+/-URS)
 
Similar Threads
How this output is coming regarding left shift operator
number converstion between(decimal, octal, Hex)
Problem in Shifting Operators
binary count to sixteen
operators