• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

bit shifting operator

 
Mathew Lee
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking at following example from link
http://www.jchq.net/certkey/0501certkey.htm


>>> This would represent -1. If you click it back one more place it would show

>>> 11111111 11111111 11111111 11111111 1111110


>>> Those examples are slightly oversimplified. Until I studied for the Java Programmers exam I had assumed that twos compliment representation only referred to the use of the leading bit to indicate the sign part of the number. As you can see it is somewhat more complex than that. To help you understand the notation a little more I have written an apparently trivial program that will show the bit pattern for a number given on the command line. It could be much improved by breaking the bits into chunks of eight, but it is still handy for getting the general picture.

public class Shift{
public static void main(String argv[]){
int i = Integer.parseInt(argv[0]);
System.out.println(Integer.toBinaryString(i));
}
}
I clearly did not understood. When i run this program by passing arguments like 11111111 11111111 11111111 11111111 1111110 I got console out put as 101010011000101011000111.
I have not understood the relation between program, arguments, output, concept.

Any ideas, resources,sample code,links, highly appreciated. thanks in advance.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not "twos complement" but "two's complement". Subtle difference.
Lots of people call the most significant bit a "sign bit", but it isn't a sign bit. It has a value of its own. That value is larger than all the other bits put together (just as the 1 in this decimal number has a bigger value than all the other digits put together: 199999999999999999999999999999999999999999999), and is regarded as negative, so it determines the sign, but it is not really a sign bit.
The most significant bit in the IEEE754 representation of -123.45 is a sign bit; if you change it from 1 to 0 the value changes to (+)123.45.
I can't work out what the example is supposed to show; you don't show which bit operator you are using. Also, if you look in the Java™ Language Specification, you find that the bit-shift operator only uses the rightmost however many bits, so a right-hand operand of -1 is converted by the "mask" to +31 (for an int or +63 for a long).

If you pass a command-line argument of 11111111 that means eleven million one hundred and eleven thousand one hundred and eleven. It is a decimal number. Note the "binary String" method does not fill in the number with leading 0s, so a positive number will show up as fewer than 32 digits. the other four inputs are not used.
By the way: why are you writing your binary numbers with 40 digits?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic