给经常使用十六进制的人一个问题
每当我使用十六进制值时,我都会在脑海中或在转换器的帮助下将其转换为十进制(如果需要)。好吧,我在想,如果您工作足够长的时间并习惯了十六进制,您是否会像读取十进制一样“看到”该值(无论这意味着什么)?我的意思是,一段时间后您是否能够像读取 10 进制值一样轻松地读取 16 进制值?当我用十六进制值编写代码时,我这样做是因为它看起来很性感而且很糟糕(有时是因为 8 位适合两位数,这有时会有所帮助),而不是因为我对它们感到某种舒服。这样我就可以知道尝试习惯十六进制是否有任何意义,以便有一天能够轻松地使用它们。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
大脑的工作方式是,如果你用它做很多事情,它就会变得更好。有些人声称“它就像肌肉”,但这有点过于简单化了。
所以,是的,如果您日复一日地进行十六进制转换,您最终将能够立即转换相当大的数字,并且您可以进行十六进制算术,而无需转换为十进制或转出十进制。
我曾经花了一个晚上的时间来记住圆周率的前 100 位数字。
所以呢?
爱因斯坦被引述为从不记住任何电话号码。他说:“我有电话簿就是为了这个。”
这里同样的交易。如果您想给自己和朋友留下深刻印象,当然,为什么不呢?但这只是一种心理锻炼,而不是真正有用的技能。
在编码中,除非您主要使用汇编程序,否则您很少会使用十六进制数字。在这些数字需要进行位处理的情况下使用十六进制数字是有意义的。例如,您可以通过与 0xFF 进行 AND 运算来屏蔽一个字节。与按位移位、符号位、OR 和 XOR 相关的数字作为十六进制也可能很有意义。用于日常算术的数字通常最好用十进制表示。
一旦你完成了客厅技巧,编程最重要的目标之一就是编写其他人(也许有一天你)可以轻松阅读和理解的代码。在正确的地方使用十六进制是好的;在错误的地方使用它会造成混乱并且不好。
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.
你不应该使用十六进制,因为“它看起来很性感而且很糟糕(有时是因为 8 位适合两位数字)”。相反,使用任何更合适的数字系统。
当您使用二进制位(例如位掩码、标志等)时,十六进制更合适,因为它们与二进制密切对应,并且(通过一些练习)您可以快速识别十六进制中的每个位数字。
当处理“正常”的现实数字时,十进制更合适。
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.
这是一个很好的问题(给你+1),就我个人而言,我使用十六进制来描述颜色,并且我每天都在使用它,已经持续了 15 年 - 所以现在我可以通过查看十六进制值来让我的朋友和同事感到惊讶说“那是紫红色”之类的话。所以,是的,我的想法是,如果你长期以某种方式使用某种东西,你的大脑最终会为你弥合这种差距。
what a great question (+1 to you), personally, i use hex to describe color, and i've been using it every day going on 15 years – so now i can amaze my friends and colleagues by looking at a hex value and saying something like "that's fuchsia". so yes, what i'm thinking is, if you use something a certain way for a long time, your brain will eventually bridge that gap for you.
在处理标志时,我总是使用十六进制。当处理例如字符的代码时,我总是使用十六进制。在处理实际数字时,我使用十六进制或十进制,具体取决于上下文的要求。
When dealing with flags, I always use hex. When dealing with codes for e.g. characters, I always use hex. When dealing with actual numbers, I use hex or decimal, depending on what the context asks for.
我认为您不需要仅仅为了练习而练习使用十六进制数字。如果您需要了解十六进制的考试,请进行练习。如果您正在面试的工作可能需要处理协议字段或在寄存器中设置位,那么请练习。
如果您在日常编码中经常使用十六进制数字,您最终将获得您正在寻找的忍者技能。如果您不经常使用它们,那么您可能不需要这些技能。我不会强迫它。
I don't think you need to practice using hex numbers just for the sake of practicing. If you have an exam that you need to know hexadecimal for, practice. If you are interviewing for a job where you might need to deal with protocols fields or set bits in a register, then practice.
If you use hex numbers often enough in day to day coding, you will eventually acquire the ninja skills you are looking for. If you don't use them that often, then you probably don't need those skills. I wouldn't force it.
许多年前,我主要从事汇编工作,并决定我需要学习十六进制的基本知识(乘法和加法表)。所以我写了几张图表并将它们贴在墙上并开始学习。我的经理走过来,问我它们是什么,然后让我出去买一个十六进制计算器!我从未了解过这些事实,但我不禁觉得在这种情况下这样做是个好主意。而且我认为做到这一点也不会太难。
话虽如此,我同意其他答案 - 在正确的上下文中使用十六进制,主要目标应该是使代码易于理解。
I was working predominantly in assembler many years ago and deciding that I needed to learn my basic facts in Hex (multiplication and addition tables). So I wrote up a couple of charts and stuck them to my wall and started learning. My manager walked by, asked me what they were and then told me to go out and buy a Hex calculater! I never did learn those facts, but I can't help feeling it would have been a good idea to do so, in that context. And I don't think it would have been too hard to do it either.
Having said this, I agree with the other answers - use Hex in the right context, main aim should be to make code understandable.
您可能会感兴趣,八进制系统是由瑞典国王查理十二世发明的,他拥有所有本来打算把他们强行带到这个国家,但两年后就发生了可怕的死亡事件。
It might interest you that the octal system was invented by King Charles XII of Sweden, who had every intention of forcing them on the country, but had a terrible case of death two years afterwards.
对不起。我内心的极客接管了。
我必须给你做这个。
http://pap.centelia.net/
这是我开发的一个 Javascript 小游戏,旨在帮助你成为坏蛋在 ol' 十六进制。
是的,你可以作弊,但这不是重点!
I'm sorry. The geek in me took over.
I had to make you this.
http://pap.centelia.net/
It's a little Javascript game I knocked up to help you become badass at the ol' hexadecimal.
Yes, you can cheat, but that's not really the point!
如果您使用十六进制足够长的时间,您最终会习惯它,是的。就像你小时候习惯了十进制一样。例如,当您因为“pi 不等于 3.243f6a88...”或类似的原因而未能通过数学课程时,请不要抱怨。
另外,@oefe,在处理“正常”的现实生活中的数字时,十进制并不更合适,它只是更受欢迎。
If you use hexadecimal for long enough, you eventually get used to it, yes. Just like you got used to decimal when you were a kid. Just don't complain when you, for example, flunk a math course because "pi is not equal to 3.243f6a88..." or something of that sort.
Also, @oefe, decimal is not more appropriate when working with "normal", real-life numbers, it is simply more popular.
好问题!
我认为就 Phinary http://en.wikipedia.org/wiki/Golden_ratio_base 说
真的虽然我有一位建筑学教授不需要反汇编程序 - 只需要一个十六进制编辑器:)
Great Question!
I think in terms of Phinary http://en.wikipedia.org/wiki/Golden_ratio_base
Seriously though I had an architecture prof who did not need a disassembler - only a hex editor :)