• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

char literal: confused about signed and unsigned

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

char c = 12; //works fine
char d = (char)-12; //works fine too

But then, the property of a char is that it is an unsigned 16 bit literal. So I was thinking that a char can only take values from 0 to 65535 (2^16 - 1), which obviously does not seem to be the case.

Can someone correct me please?

Thanks,
-Vijay
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I think by casting, you're converting -12 to an equivalent char. Have you tried printing the result from that casting? What is it?
 
Vijay Gade
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Bimal,

Thanks for the quick response. I was about to post my findings:

Actually, one thing I missed was that, a char is a signed 16 bit INTEGER literal under the "hood" -- whatever hood meant :-)

So,

char a = 98;
is the same as
char b = (char)65634; // becausee 65634 = 65536 + 98, where 65536 = 2^16.

OR

char c = (char)-98;
is the same as
char d = 65438; //because 65438 = 65536 (which is 2^16) - 98.

So there's a roll over thing going on if the number assigned to a char exceeds 2^16-1 or is less than -2^16, but of course, there has to be a casting to (char), AND, the number should not exceed 2^31-1 or be less than -2^31.

Thanks,
-Vijay
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. Vijay.

-12 is an integer number , you are converting it into equal charcter value.

sekhar scjp 1.4
[ February 08, 2006: Message edited by: Chandrasekhar Mangipudi ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The compiler allows assignment of a larger integer literal to a smaller integer type if the value fits into the smaller type. 12 is an int literal. This may be regarded as an implicit narrowing conversion performed by the compiler.



When explicitly converting a int literal to a char, the first 16 bits of the int literal is truncated. All int's are represented in two's complement and truncating the first 16 bits leaves us with the value 2^16-12.

/Per
[ February 08, 2006: Message edited by: Per Idahl ]
reply
    Bookmark Topic Watch Topic
  • New Topic