• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Mock Exam Fundamentals 1 - questions 6,7

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wanna know how I can convert to hex this numbers?? I think that have an easy way to do it that I don't know...
Thanks for any help
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
u mean converting a hexadecimal number to decimal number?
its as follows
to convert a hexadecimal like 0x1234 to binary
1x16^3 + 2x16^2 + 3x16^1 + 4x16^0=4660
 
Isabel Wanderley
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Humm.. no from hex to dec I know... but convertions like:
Integer.toHexString(Integer.MIN_VALUE)
Integer.toOctalString(121)
I don't think I'd have to convert to dec and then convert to hex/oc, I think there is an easy way to do it... and I don't know how...
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Isabel Wanderley:
I wanna know how I can convert to hex this numbers?? I think that have an easy way to do it that I don't know...
Thanks for any help


Question 6 is as follows.

To answer this question you need to know three things about each of the three data types--byte, short, and char.
First, You have to know the size of the primitive types. Type byte is 8 bits and char and short are both 16 bits.
Second, you need to know that char is an unsigned type and byte and short are signed types.
Third, you need to know the two's compliment representation of the integral primitive types.
Since byte is an 8 bit signed value, then the MAX_VALUE must be represented as follows in binary.
01111111
The sign bit is zero and the rest of the bits are one.
The above binary value is extremely easy to convert to hexadecimal. Each hex digit represents four binary bits so the left four bits represent one hex digit and the right four bits represent the second hex digit.


Binary 0111 1111
Hex 7 f


Char is an unsigned 16 bit value so the binary representation of Character.MAX_VALUE is as follows.


Binary 1111 1111 1111 1111
Hex f f f f



Short is a signed 16 bit value so the binary representation of Short.MAX_VALUE is as follows.


Binary 0111 1111 1111 1111
Hex 7 f f f

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

Originally posted by Isabel Wanderley:
Humm.. no from hex to dec I know... but convertions like:
Integer.toHexString(Integer.MIN_VALUE)
Integer.toOctalString(121)
I don't think I'd have to convert to dec and then convert to hex/oc, I think there is an easy way to do it... and I don't know how...


Octal and Hexadecimal are used with computers because they are very easy to convert to and from binary. Octal was very useful when most computers were 8 bit machines. Hexadecimal is easier when multiple bytes are converted.
Here's a question that prints Byte.MAX_VALUE in binary, octal, decimal, and hex.

The binary representation of Byte.MAX_VALUE is as follows.
01111111
The Integer.toBinaryString method does not print the leading zero. Instead, it only prints the seven bits that are set to one as follows.
1111111
To convert the binary byte to octal just divide the byte into three nibbles.

To convert Byte.MAX_VALUE to hexadecimal just divide the byte into two four bit nibbles.

To convert Byte.MAX_VALUE to decimal you could just raise 2 to the seventh power and then subtract one. The result is 127.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope that my earlier explanations were helpful. I just wanted to use this post to explain why I put those questions on the exam.
TheSCJP Exam Objective, SECTION 4: LANGUAGE FUNDAMENTALS, states the following.


State the range of all primitive formats, data types and declare literal values for String and all primitive types using all permitted formats bases and representations.


Questions six and seven ask for the hex represenation of the range for two reasons. First, the hex representation is easier to remember than decimal and because the ability to work in binary, octal, decimal and hex is an exam requirement.
 
Isabel Wanderley
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lottttttt... U really helped me
 
What's a year in metric? Do you know this metric stuff tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic