# octal, hex

Brian Smith

Ranch Hand

Posts: 232

sasank manohar

Ranch Hand

Posts: 186

posted 15 years ago

A) To convert a number 10 to octal (0-7)

1. Divide the number by 8 you will get 1 and reminder is 2

10/8=1 , 10%8=2

2. Just combine 1 and 2 that is 12

B) To convert a number 20 to hexadecimal(1-F)

Apply same steps as used for octal conversion(Step 1 and 2 above)

20/16=1 , 20%16=4

So Hex representation of 20 is 14.

C) To convert oct to dec

1*8^1 + 2*8^0=10

D) To covert hex to dec

1*16^1 + 4*16^0=20

[This message has been edited by sdev (edited August 15, 2000).]

1. Divide the number by 8 you will get 1 and reminder is 2

10/8=1 , 10%8=2

2. Just combine 1 and 2 that is 12

B) To convert a number 20 to hexadecimal(1-F)

Apply same steps as used for octal conversion(Step 1 and 2 above)

20/16=1 , 20%16=4

So Hex representation of 20 is 14.

C) To convert oct to dec

1*8^1 + 2*8^0=10

D) To covert hex to dec

1*16^1 + 4*16^0=20

[This message has been edited by sdev (edited August 15, 2000).]

"SCJP5 | SCWCD5| DEVELOPER"

Brian Smith

Ranch Hand

Posts: 232

sasank manohar

Ranch Hand

Posts: 186

posted 15 years ago

As you may know

>> - signed right shift(divide by 2)

>>> - unsigned right shift

<< - signed left shift(multiply by 2)<br /> <br /> >> - This operator shifts the bits to the right 1 by 1 and

Shifted off bits are lost(discarded) and 0 is brought

into the left(higher order bit).

For a negative number the sign remains and 1 is brought

into the left.

Note: Shifting -1 to the right always results in -1

<< - This operator shifts the bits to the left 1 by 1 and<br /> higher order bit which is shifted out is lost(discarded)<br /> and a 0 is brought into the right<br /> <br /> >>> - This operator shifts the bits to the right 1 by 1 and

higher order bit which is shifted out is lost and a 0

bit is brought into the right (for both +ve and -ve nos.)

Note: This operator always returns +ve value for -ve

values also

This is a brief explanation. If you really want to understand these operators , then you need to work out applying these operators for different type of values

If you have any doubts on a particular topic then you go to search in this top of the forum and type the related topic , you will get lot of answers that were discussed in this forum(just a suggestion).

thanks..

[This message has been edited by sdev (edited August 15, 2000).]

>> - signed right shift(divide by 2)

>>> - unsigned right shift

<< - signed left shift(multiply by 2)<br /> <br /> >> - This operator shifts the bits to the right 1 by 1 and

Shifted off bits are lost(discarded) and 0 is brought

into the left(higher order bit).

For a negative number the sign remains and 1 is brought

into the left.

Note: Shifting -1 to the right always results in -1

<< - This operator shifts the bits to the left 1 by 1 and<br /> higher order bit which is shifted out is lost(discarded)<br /> and a 0 is brought into the right<br /> <br /> >>> - This operator shifts the bits to the right 1 by 1 and

higher order bit which is shifted out is lost and a 0

bit is brought into the right (for both +ve and -ve nos.)

Note: This operator always returns +ve value for -ve

values also

This is a brief explanation. If you really want to understand these operators , then you need to work out applying these operators for different type of values

If you have any doubts on a particular topic then you go to search in this top of the forum and type the related topic , you will get lot of answers that were discussed in this forum(just a suggestion).

thanks..

[This message has been edited by sdev (edited August 15, 2000).]

"SCJP5 | SCWCD5| DEVELOPER"

daryl olson

Ranch Hand

Posts: 36

posted 15 years ago

The method sdev outlined works for decimal values below 80 but for values above that it there is a little bit more to do.

Consider what the numbers represent. For example:

1) The octal (base 8) number 02134 in base 10 is:

(0* (8^4) + (2*(8^3)) + (1*(8^2)) + (3*(8^1) ) + (4*(8^0)) = 1116

2) So to convert the base 10 number 1116 to octal, reverse the process:

a) 1116/(8^4) < 1 so 0 with 1116 remainder

b) 1116/(8^3) = 2 with (1116 - 1024) = 92 remainder

c) 92/(8^2) = 1 with (92 - 64) = 28 remainder

d) 28/(8^1) = 3 with (28 - 24) = 4 remainder

e) 4/(8^0) = 4

Putting the values in their position, you get: 02134

Same logic applies for hex (base 16) and binary (base 2) numbers.

Consider what the numbers represent. For example:

1) The octal (base 8) number 02134 in base 10 is:

(0* (8^4) + (2*(8^3)) + (1*(8^2)) + (3*(8^1) ) + (4*(8^0)) = 1116

2) So to convert the base 10 number 1116 to octal, reverse the process:

a) 1116/(8^4) < 1 so 0 with 1116 remainder

b) 1116/(8^3) = 2 with (1116 - 1024) = 92 remainder

c) 92/(8^2) = 1 with (92 - 64) = 28 remainder

d) 28/(8^1) = 3 with (28 - 24) = 4 remainder

e) 4/(8^0) = 4

Putting the values in their position, you get: 02134

Same logic applies for hex (base 16) and binary (base 2) numbers.