Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

conversion of negative numbers to octal/hex

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

Can anyone please help me with the conversion of decimal -20 (negative 20) to octal and hexadecimal and vice versa.

Thanks,
Sarika.
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sarikaa Bhatnagar:
Hi,

Can anyone please help me with the conversion of decimal -20 (negative 20) to octal and hexadecimal and vice versa.

Thanks,
Sarika.



Hmmm.... Interesting question. How would you do it?

Let's start with 20 in binary: (Somebody please check my math here!)

20 = 0000 0000 0001 0100

Go through 2's complement to find -20 in binary:

~20 = 1111 1111 1110 1011
~20 + 1 = -20 = 1111 1111 1110 1100

For Hexidecimal, just convert every group of 4 bits:

-20 = F F E C = 0xFFEC

For Octal, just convert every group of 3 bits:

-20 = 1 111 111 111 101 100
-20 = 1 7 7 7 5 4 = 0177754

Whew!! Hope I got my math right ...

Henry
 
Sarikaa Bhatnagar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot!!! I am really grateful.
reply
    Bookmark Topic Watch Topic
  • New Topic