• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sign extension doubt?

 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Marshal
Pie
Posts: 21414
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21414
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry. I was kind of sleepless night because I couldn't understand it.

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The (1) is not the value but a marker, indicating the line is special.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21414
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all


how come this even though the byte has to sign extension like char in above
 
Henry Wong
author
Marshal
Pie
Posts: 21414
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic