File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Vidya Selvaraj
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 242
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what you mean by "add the 1 to the above result" in rule 3?
Thanks...I am confused
 
Vidya Selvaraj
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, Thanks Rajani for pointing me that out!
Regards
Vidya
 
Santhosh Kumar
Ranch Hand
Posts: 242
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lott Santhosh for your patience.
Regards
Vidya
 
Dipali Desai
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Santosh, thanks for the code example, too bad Sun doesn't provide a toBinaryString() method that works with negative numbers.
 
Shan Karawita
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Wow ! The Code example is really great !

Well Done Santosh !
It'll really helpful for aspirants like us .
 
Sahir Shibley
Ranch Hand
Posts: 275
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Good lord. What is all this ?
 
Santhosh Kumar
Ranch Hand
Posts: 242
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the API I thought it only works with pos no. I'll have to try it with a neg. no.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic