aspose file tools*
The moose likes Beginning Java and the fly likes Explaination Required. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Explaination Required." Watch "Explaination Required." New topic
Author

Explaination Required.

Nikhil Nagrani
Greenhorn

Joined: Dec 23, 2010
Posts: 24
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nikhil Nagrani
Greenhorn

Joined: Dec 23, 2010
Posts: 24
Ya sure...from the next time i will keep that in mind...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Nikhil Nagrani
Greenhorn

Joined: Dec 23, 2010
Posts: 24
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
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

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

Joined: Oct 02, 2003
Posts: 10916
    
  12

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Nikhil Nagrani
Greenhorn

Joined: Dec 23, 2010
Posts: 24
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
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

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

Joined: Dec 23, 2010
Posts: 24
Hey..thanks henry...
 
jQuery in Action, 2nd edition
 
subject: Explaination Required.
 
Similar Threads
Masking sign extention of shift opt.
operators
A doubt in bitwise operator
Right shift using array
conversion of binary and hex to decimal and about left/right shift