This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# Representation

Arnb Sen
Ranch Hand
Posts: 145
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
Posts: 24208
35
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
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
Posts: 20995
76
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
>> 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:

Now, turn on every 4th light:

I'm waiting...

Rick Portugal
Ranch Hand
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.

Arnb Sen
Ranch Hand
Posts: 145

Layne Lund
Ranch Hand
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