• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Sign extension doubt?

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23947
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23947
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry. I was kind of sleepless night because I couldn't understand it.

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22773
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The (1) is not the value but a marker, indicating the line is special.
 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23947
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22773
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23947
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
You got style baby! More than this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic