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

Hexa / Octa Conversions.

 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On don's sites questions like these puzzle me....
Are we supposed to do the conversions and check the decimal value each time such questions are posed :roll: ( is there a shorter method ) - And same goes for unicode values assigned to a char> how do we ascertain if its assignable or not?
===========
<Find errors in the lines below >


or below:


Thank you so much..
Shivani
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Shivani

well before doing for the conversion to decimal you can check for the size of the assigned operand
//first case
char c1 = 0xffff;
a character is an unsigned 16 bit integer
every hexadecimal bit represents 4 binary bit's (f==1111)
hence char c1=0xffff is valid
//second case
char c2 = 0xfffff;
the value being assigned is 20 bit long and a char can accept on 16 bits
hence u get the error
//third case
byte b1 = 0xffff;
byte is a 8 bit number and the above code tries to assign a 16 bit number to it hence you get the error
//forth case
well this is tricky and i am still working on as to why did u get an error
cos 0x7f is 0111 1111 = 127
are you absolutely sure you got an error???
the other two are fine

you can use this to eliminate options and to be sure with the remaining i guess you have to do it the hard way


Regards
Simon
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
char c1 = 0xffff; // 1 OK
char c2 = 0xfffff; // 2 ERROR
byte b1 = 0xffff; // 3 ERROR
byte b2 = 0x7f; // 4 ERROR
byte b3 = 0xff; // 5 OK
byte b4 = -0x80; // 6 OK

I can see how c2, b1, and b3 (the ones in boldface) have problems; the rest are fine.

In each problem case, the value assigned would lose precision if the compiler allowed it. The char type is 16 bits wide. A hex number requires 4 bits for each column in the value. That is, 0xfffff requires 20 bits; the char type can only hold 16 of them. It's a bad fit.

The byte type can only hold 7 bits of value, since the most significant, or high-order, bit is used to express positive or negative value. So you can fit in 0x7f but not 0xff.
[ September 06, 2005: Message edited by: Michael Ernest ]
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the broken record dept:

Remember - this topic applies only to the SCJP 1.4 exam... this topic is NOT on the SCJP 5.0 exam...

carry on
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shivani
byte XX = 0xff would result in compiler error.
because 0xff =255. 0x7f = 127.
127 is within the range.but 255 is not within the range of byte i.e -128 to 127.
0xff = 1111 1111;
0x7f = 0111 1111;

Can Anyone explain why -1 and 255 have same binary number 1111 1111???

Shanthi
 
Evacuate the building! Here, take this tiny ad with you:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic