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

Sign extension doubt?

Deepak Borania
Ranch Hand

Joined: Jul 28, 2009
Posts: 45

I understand that the output 655535 is caused due to sign extension in statement (1), but what is happening in statement (2) that prevents this. Width of byte is 8 bits, so 'anding' it with 0xff should just result in byte value unchanged, shouldn't it?

BTW how can I print 'byte' as binary or hex strings?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18835
    
  40

Width of byte is 8 bits, so 'anding' it with 0xff should just result in byte value unchanged, shouldn't it?


Not really. 0xff is a int literal. So the result is indeed the original bit value from the byte, but now stored in an int.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18835
    
  40

BTW how can I print 'byte' as binary or hex strings?


One option is to first convert it to a binary or hex string using the java.lang.Integer class. Another option is to use the System.out.printf() method call.

Henry

Deepak Borania
Ranch Hand

Joined: Jul 28, 2009
Posts: 45
Thanks Henry. I was kind of sleepless night because I couldn't understand it.

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
char c= (char)(b); //(1)
System.out.println((int)c); //output: 65535

Can you please tell me how we get the out put 65535 i'm confused


Creativity is nothing but Breaking Rules
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Does this Java™ Language Specification section help at all? Note, if you wind back about three lines, you see that
char . . . values are 16-bit unsigned integers
If you need more explanation, please ask again.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
santhosh.R gowda wrote:char c= (char)(b); //(1)
You mean -1 surely? There is a lot of difference between 1 and -1. Thirty-one "1" bits' worth, in fact.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

The (1) is not the value but a marker, indicating the line is special.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Rob Prime wrote:The (1) is not the value but a marker, indicating the line is special.
Thank you, Rob. I should know better than to try reading that sort of code after beer
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18835
    
  40

santhosh.R gowda wrote:
char c= (char)(b); //(1)
System.out.println((int)c); //output: 65535

Can you please tell me how we get the out put 65535 i'm confused


Easy part, a byte is signed 8 bit value, and a char is a unsigned 16 bit value. Hard part, you need to understand how Java stores negative numbers... see twos complement...

http://en.wikipedia.org/wiki/Two's_complement

Once you understand twos complement, and how a byte is sign extended when casting to a char, it should be straightforward. However, if you are still confused (after you get up to speed on twos complement), come back here, and elaborate what you don't understand.

Henry
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
come back here, and elaborate what you don't understand.


Dear sir, as you stated above that

please tell me what is the integer value stored in c before type casting into int.
please rectify the below code

please give me debugging details
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Just as 1111 1111 is 255, 1111 1111 1111 1111 is 65535. After all, its 32768 + 16384 + 8192 + 4096 + 2048 + 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1.
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Dear all


how come this even though the byte has to sign extension like char in above
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18835
    
  40

santhosh.R gowda wrote:Dear all


how come this even though the byte has to sign extension like char in above


b = 1111 1111
i (sign extend b to length of int) = 1111 1111 1111 1111 1111 1111 1111 1111

Remember an int is signed, and a char isn't (when applying twos complement)....

Henry
Deepak Borania
Ranch Hand

Joined: Jul 28, 2009
Posts: 45
Take look in "Java Puzzlers: Traps, Pitfalls, and Corner Cases"- Puzzle 6 for more info on the topic.
It was the puzzle from which this question orignated anyway.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sign extension doubt?