File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Representation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Representation" Watch "Representation" New topic
Author

Representation

Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
Why does Java support representation in Octal and Hexadecimal ? Any particular advantage ? Why can't we use decimals ?


Regards,<br />Arnab
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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."


[Jess in Action][AskingGoodQuestions]
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
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
Sheriff

Joined: Sep 28, 2004
Posts: 18522
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
>> 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

Joined: Dec 17, 2002
Posts: 243
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.


IBM 286, SCJP, SCWCD, EIEIO
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
Thanks for all your replies.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Representation
 
Similar Threads
octal and hexa representation
jQuery question
tokenizer
mock doubt(DAN)
UML Representation for design pattern