Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A question for you who work with hexadecimal frequently

Tags:

decimal

hex

Whenever I work with hexadecimal values, I do the conversion (if needed) into decimal in my head or with the help of a converter. Well I was thinking, if you work long enough and get used to hexadecimal, do you just 'see' the value (whatever that means), as when reading decimal ? I mean are you after a while able to read base 16 values as easily as base 10 values ? When I write code with hexadecimal values, I do it because it looks sexy and badass (and sometimes because 8 bits fits into two digits, which helps sometimes), not because I feel somehow comfortable with 'em. That way I could know if there is any point in trying to get used to hexadecimal, to be some day able to work with them with ease.

like image 235
Hoffa Avatar asked Jan 11 '10 20:01

Hoffa


People also ask

How is hexadecimal used in everyday life?

Hexadecimal can be used to write large binary numbers in just a few digits. It makes life easier as it allows grouping of binary numbers which makes it easier to read, write and understand. It is more human-friendly, as humans are used to grouping together numbers and things for easier understanding.

What is hexadecimal commonly used for?

Hexadecimal is used extensively in assembly programming languages and in machine code. It is often used to refer to memory addresses. It can be used during the debugging stage of writing a computer program and to represent numbers stored in a CPU's registers or in main memory.

Why do technicians prefer to use hexadecimal?

Hexadecimal uses digits that more closely resemble our usual base-10 counting system and it's therefore easier to decide at a glance how big a number like e7 is as opposed to 11100111. Higher information density. With 2 hexadecimal digits, we can express any number from 0 to 255.

What are the things you should remember in using hexadecimal value?

Start with the right-most digit of your hex value. Multiply it by 160, that is: multiply by 1. In other words, leave it be, but keep that value off to the side. Remember to convert alphabetic hex values (A, B, C, D, E, and F) to their decimal equivalent (10, 11, 12, 13, 14, and 15).


2 Answers

The way the brain works, it gets better if you do something a lot with it. Some people claim "it's like a muscle" but that's simplifying a bit too far.

So yeah, if you do hex conversions day in and out, you'll eventually be able to convert reasonably large numbers on sight, and you can do hex arithmetic without needing to convert into and out of decimal.

I once spent an evening memorizing the first 100 digits of pi.

So what?

Einstein is quoted as never memorizing any phone numbers. He said, "that's what I have a phone book for."

Same deal here. If you want to impress yourself and maybe your friends, sure, why not. But it's a mental exercise, not a truly useful skill.


In coding, unless you're working predominantly with assembler, you'll be using hex numbers only rarely. It makes sense to use hex numbers in contexts where those numbers will be bit-twiddled. e.g. You might mask a byte by AND-ing against 0xFF . Numbers associated with bitwise shifting, sign bits, OR and XOR may also make a lot of sense as hex. Numbers used for day-to-day arithmetic will usually be best expressed in decimal.

Once you're done with parlor tricks, one of the most important goals of programming is to write code that others (and maybe some day you) can read and understand easily. Using hex in the right places is good; using it in the wrong places is obfuscation and bad.

like image 107
Carl Smotricz Avatar answered Oct 05 '22 12:10

Carl Smotricz


You shouldn' use hex because "it looks sexy and badass (and sometimes because 8 bits fits into two digits)". Instead, use whatever number system is more appropriate.

Hexadecimal is more appropriate when you are working with binary bits (e.g. bit masks, flags etc,), since they closely correspond to binary, and (with some practice) you can quickly recognize each it in a hexadecimal number.

Decimal is more appropriate when working with "normal", real-life numbers.

like image 43
oefe Avatar answered Oct 05 '22 14:10

oefe