• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

shift opeartors - need help

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

i have some doubts on the operations of shift operators... Can some one

pls answer how we get sucha result.

a) public class test {
public static void main(String args[]) {
byte x = 3;
x = (byte)~x;
System.out.println(x);
}
}


The answer is -4

b) public class test {
public static void main(String args[]) {
int x;

x = -3 >> 1;
x = x >>> 2;
x = x << 1;
System.out.println(x);
}
}

The answer is 2147483646

c) public class test {
public static void main(String args[]) {
int i = -1;
i = i >> 1;
System.out.println(i);
}
}

The answer is -1

Basically i have a doubt as how the negative numbers like -3 etc will

be represented in binary. Also how the ~ works.

please help me folks...
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Smarty,
In java -ive numbers are represented in twos compliment arithmetics. That is the binary representation of a number can be obtained by inverting the bit representation of the number and adding 1 to the result. Thus -3 will be represented as-
3 in binary is 00000011
now for - 3 invert the bits first
which will be 11111100
now add 1 to the result. thus 11111101 this is the binary reprentation of -3 in a 8 bit format. (i took 8 bits for ease of representation).
Bitwise operators are very easy to use i will tell u some basic rules of finding the results.
~x = (-x)-1 // this rule is always true thus ~3 = (-3) -1 = -4
x >>n = x/(2 power of n)// 9>>2 = 9 / (2 power 2) = 9/4 = 2;
x<<n = x * (2 power of n)//
Please let me know if further clarifications are required.
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

a) public class test {
public static void main(String args[]) {
byte x = 3;
x = (byte)~x;
System.out.println(x);
}
}



The bit value of the byte 3 is

00000011

~ is the complement operator

The result of ~3 is

11111111111111111111111111111100

Casting it to a byte we get

11111100

Which is the representation of -4.

b) public class test {
public static void main(String args[]) {
int x;

x = -3 >> 1;
x = x >>> 2;
x = x << 1;
System.out.println(x);
}
}



The representation of -3 is

11111111111111111111111111111101

The first shift gives

11111111111111111111111111111110

The second shift gives

00111111111111111111111111111111

The third shift gives

01111111111111111111111111111111

The value is

Integer.MAX_VALUE

c) public class test {
public static void main(String args[]) {
int i = -1;
i = i >> 1;
System.out.println(i);
}
}



The bit representation of -1 is

11111111111111111111111111111111

The shift gives

11111111111111111111111111111111

which is -1.

Negative numbers are stored using 2's complement.
 
ranger
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"smarty"

Change your display name back to your real first and last names.

Mark
 
author
Posts: 8998
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,

Also remember that this is a topic on 1.4, but it's NOT a topic on the 1.5 exam.
 
Their achilles heel is the noogie! Give them noogies tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!