Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Explaination Required.

 
Nikhil Nagrani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey could anybody please explain me the output of the following codes and how hexadecimal string representation works.Thanks.

1)

The program generates the following output:
536870908
1073741816
2147483632
-32

2)Masking sign extension.

Here is the output of this program:
b = 0xf1
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Please UseCodeTags next time. I've added them for you this time. Don't forget to keep the indentation (which I also added).
 
Nikhil Nagrani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya sure...from the next time i will keep that in mind...
 
Henry Wong
author
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Nagrani wrote:
1)

The program generates the following output:
536870908
1073741816
2147483632
-32



First, do you know the difference between base 10 and base 16? Second, do you know how shifting works? And finally, do you know what twos complement does?

Those concepts should explain everything in this example. You should start your research there.

Henry
 
Nikhil Nagrani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Henry,

I have gone through those concepts but unable to get through these examples(rather this type of hexadecimal notation)......please explain me the same.Thanks..
 
Henry Wong
author
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Nagrani wrote:
I have gone through those concepts but unable to get through these examples(rather this type of hexadecimal notation)......please explain me the same.Thanks..


Please explain the confusion that you are encountering, in relation to those concepts -- and hopefully, we can figure out where you went wrong.

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Nagrani wrote:unable to get through these examples(rather this type of hexadecimal notation)....

What do you mean by "this type of hexadecimal notation"? What other kind is there?
 
Nikhil Nagrani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what i can interpret:
Num=0xFFFFFFE
i.e num is 1111 1111 1111 1111 1111 1111 1110
So goin through the loop,after 1st iteration,num should be left shifted by one,so num becomes:
num: 1111 1111 1111 1111 1111 1111 1100
So now how can this be equal to 536870908(1st output)...

I guess i m conceptually wrong somewhere..correct me please.
 
Henry Wong
author
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Nagrani wrote:This is what i can interpret:
Num=0xFFFFFFE
i.e num is 1111 1111 1111 1111 1111 1111 1110
So goin through the loop,after 1st iteration,num should be left shifted by one,so num becomes:
num: 1111 1111 1111 1111 1111 1111 1100
So now how can this be equal to 536870908(1st output)...

I guess i m conceptually wrong somewhere..correct me please.


An int is 4 bytes... So, 0xFFFFFFE is binary 0000 1111 1111 1111 1111 1111 1111 1110. And after the shift it becomes binary 0001 1111 1111 1111 1111 1111 1111 1100.

Henry
 
Nikhil Nagrani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey..thanks henry...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic