Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

integral literals ?

 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i,am confused about the integral literals .
To express 28 ( which is expressed in decimal ) in octal , we can prefix the literal with
0(zero). To indicate 28 to hexadecimal we prefix it 0X or 0x.
please explain how 28 when expresses on octal becomes 034
and how 28 when expressed in hexadecimal becomes 0x1c.
thanks in advance
regards
raghav...
 
swapna sivaraju
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

To convert a decimal number to octal we do something like this--
see the representation of 28 in binary is--
let byte b=28;
then,28--00011100
now--split the above into 3 fragaments (for octal) each starting from back..ie.
000 011 100
1 2 3

now do the same as we do to convert the above binary to decimal..ie.
1 represents--0 (2^2*0+2^1*0+2^0*0)
2 represents--3--(2^2*0+2^1*1+2^0*1)
3represents-- 4 (2^2*1+2^1*0+2*2^0*0)
so now that's how we get 034 as octal representation for 28 .
for converting to hexa fragament the binary representation into 4 parts each and calculate.
ie. --0001 1100
1 2
1 ---2^0*1 = 1
2--- 2^3*1+2^2*1+0+0 = 12

and for hexadecimal we represent 12 as c
(10-15 are represented by a-f)
so the hexa value is 0x1c

hope this helps.
swapna
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by raghav mathur:
hi
i,am confused about the integral literals .
To express 28 ( which is expressed in decimal ) in octal , we can prefix the literal with
0(zero). To indicate 28 to hexadecimal we prefix it 0X or 0x.
please explain how 28 when expresses on octal becomes 034
and how 28 when expressed in hexadecimal becomes 0x1c.
thanks in advance
regards
raghav...

We're changing bases here, Raghav. We see 28 in decimal but, in octal, that is 34. You see, to get the value of a number, you take each digit and multiply by the base to the power of the position. Let's look at decimal to begin with:

You can perform a similar operation in the other bases, such as octal (base 8) or hexadecimal (base 16) or binary (base 2):

If you do a search in this forum, I'm sure you'll find all sorts of information on changing base.
Corey
 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
thanks a lot for responding . I,am not very comfortable with the explaination you gave
I've got the thumb rule , that for converting decimal to octal we are suppose to
divide the binary representation of 28 (00011100) into 2 parts . I,am confused
about the calculation which you've done . i.e , how does 1 represents 0 ,
2 represents 3 and 3 represents 4 .
Moreover , convertin decimal to hex , how does 1 represents 1 and 2 represents 12.
please exlplain in detail .
thanks for your patience
regards
raghav mathur
Originally posted by swapna sivaraju:
Hi

To convert a decimal number to octal we do something like this--
see the representation of 28 in binary is--
let byte b=28;
then,28--00011100
now--split the above into 3 fragaments (for octal) each starting from back..ie.
000 011 100
1 2 3

now do the same as we do to convert the above binary to decimal..ie.
1 represents--0 (2^2*0+2^1*0+2^0*0)
2 represents--3--(2^2*0+2^1*1+2^0*1)
3represents-- 4 (2^2*1+2^1*0+2*2^0*0)
so now that's how we get 034 as octal representation for 28 .
for converting to hexa fragament the binary representation into 4 parts each and calculate.
ie. --0001 1100
1 2
1 ---2^0*1 = 1
2--- 2^3*1+2^2*1+0+0 = 12

and for hexadecimal we represent 12 as c
(10-15 are represented by a-f)
so the hexa value is 0x1c

hope this helps.
swapna
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please explain how 28 when expresses on octal becomes 034
and how 28 when expressed in hexadecimal becomes 0x1c.


28 / 8 = 3 remainder 4
3 * 8^1 + 4 * 8^0

28 / 16 = 1 remainder 12
1 * 16^1 + 12 (notatated as 'c' in hexadecimal) * 16^0.
 
swapna sivaraju
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oopss.... some typo error..

The representational for 28 is
00011100
the 3 parts are--
1) 000 which is 0
calculated as (0*2^2+0*2^1+0*2^0)
2) 011 which is 3
calculated as (0*2^2+1*2^1+1*2^0)
3) 100 which is 4
calculated as (1*2^2+0*2^1+0*2*2^0)
So now we get the octal form as 034 for 28.got it now??
i hope its clear now
swapna
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic