Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Representation

 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does Java support representation in Octal and Hexadecimal ? Any particular advantage ? Why can't we use decimals ?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hex is very useful when you're doing certain operations -- manipulating bits, for example. One reason why they're useful is because one hex digit is always exactly 4 bits. Decimal numbers don't have this property, but hex numbers do. Octal numbers are always three bits, which is potentially useful, too. Personally I've never used octal, and never needed to -- but it used to be common in the "good old days."
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok. so what I understand is hexadecimals are important as it helps in manipulating bits. Why do we need to manipulate bits ? Manipulating bits will result in new number. So can't we have a formula which will manipulate decimal numbers to give the same output as achieved by manipulating the bits i hexadecimal numbers ?
 
Henry Wong
author
Marshal
Pie
Posts: 20894
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arnb Sen:
ok. so what I understand is hexadecimals are important as it helps in manipulating bits. Why do we need to manipulate bits ? Manipulating bits will result in new number. So can't we have a formula which will manipulate decimal numbers to give the same output as achieved by manipulating the bits i hexadecimal numbers ?


Wow... it has been decades since I heard this argument! I remember years ago, when a colleague argued, why the heck do we need C? What is so special about while loops? or switch statements? There is nothing that couldn't be done in C that couldn't be done in assembly language.

Back then, I didn't have a good answer... today... I may have one. Quite frankly, because the most expensive part of a development system is the person behind the keyboard. And anything you can do to make the programmers life easier is a good idea. Can you do everything in decimal? Sure you can. But why? If the algorithm is designed around bits, such as graphics, bitwise operators are very useful.

Henry
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>> we can achieve the same result with decimal...

Here is a bit mask where each bit turns on a given light. In case it's not obvious, this turns on every other light:

int mask = 2863311530;

Now, turn on every 4th light:

int mask = ...

I'm waiting...
 
Rick Portugal
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arnb Sen:
Why do we need to manipulate bits ?
Bit-level manipulation is low-level programming done in graphics and systems programming. I learned about it to get my certification but have never used in the real world. If you are developing a game or something it would come in handy.
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your replies.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Hex is very useful when you're doing certain operations -- manipulating bits, for example. One reason why they're useful is because one hex digit is always exactly 4 bits. Decimal numbers don't have this property, but hex numbers do. Octal numbers are always three bits, which is potentially useful, too. Personally I've never used octal, and never needed to -- but it used to be common in the "good old days."


I don't think I've ever used octal in a program, whether it's in C++ or Java. However, I use it occassionally when I have to use the Unix chmod tool.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic