编程与软件工程catonmat.net
你必须知道的底层位运算技巧
Low Level Bit Hacks You Absolutely Must Know
2009 — 2018,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这篇文章收集了一系列实用的底层位运算技巧,例如用 x & 1 判断奇偶、用 x & (x-1) 清除最低位的 1、交换整数等。每个技巧都配有解释,帮助读者理解位操作的原理。
为什么经久不衰:位运算是计算机底层思维的基础,这些技巧跨越十几年仍被反复推荐,是理解编译器优化和底层编程的经典入门材料。
HN 评论精选
这些技巧是否"必须知道"取决于工作领域,不懂也不妨碍成为好开发者
我曾有一次面试被问到"判断整数奇偶"这类问题。12 年后,我招过的人里没有一个能正确回答这个问题,但他们几乎都是很优秀的开发者。所以"必须掌握"完全取决于你写什么类型的软件。
throwaway2016a查看英文原文
I was asked #1 "Check if the integer is even or odd" in an interview once. Not worded quite that way but that was the answer they were looking for. 12 years later not a single person I have hired could correctly answer that question[1] and most of them (almost all) have been great productive software developers. So "absolutely must know" is entirely dependent on the type of software you write. [1] I don't ask it in the interview, I ask it after they are hired and have been with us a few weeks just for fun.
区分"hack"与常规位操作:很多技巧可读性差,现代代码中未必值得用
第一个技巧(判断奇偶)确实算 hack,但现实中没人会用它而不用 n % 2,后者几乎人人一看就懂。其余的其实不算 hack,而是位操作本身——对需要的人至关重要,对不需要的人毫无用处。
askvictor查看英文原文
The first one (check if an integer is odd or even) is a hack, sure. But I can't think of anywhere you'd use it in the real world over (n % 2), which almost everyone will understand much quicker what you're doing. The rest aren't hacks, they're bit manipulations, which are crucial for those who need them, and just about useless to those who don't.
现代编译器会自动优化,这些技巧的价值在于语义而非性能
尽管我很喜欢位操作,但必须说大部分技巧对 C/C++ 代码并没有真正的性能提升(只是写出难懂代码的极客快感)。比如任何像样的编译器下,(x & 1) == 0 和 (x % 2) == 0 性能相同。不过当变量需要按位而非按数值处理时,位操作确实非常方便。
bhavin查看英文原文
As much as I love bit manipulation, I have to say that most (not all) of them don't provide any real performance gain for C/C++ code (only some geeky satisfaction of writing difficult to interpret code!).. e.g. with any decent compiler... * if ((x & 1) == 0) performs same as, if ((x % 2) == 0) * if (x & (1<<n)) same as, if (x% (pow(2,n))) so on.. on the other hand, i find bit operations really handy when the variables in question are to be treated as separate bits than normal base-10..
推荐《Hacker's Delight》作为更系统的进阶读物
如果你想深入了解这些技巧及其原理,我强烈推荐 Henry S. Warren 的《Hacker's Delight》一书。我日常工作中就用过其中不少技巧。
CountHackulus查看英文原文
If you're interested in reading about more of these, and how they work I'd highly suggest reading the book Hacker's Delight by Henry S. Warren[1]. I know that I've used more than a few of the tricks in my day-to-day work. [1] http://www.amazon.com/Hackers-Delight-Henry-S-Warren/dp/0201914654/ref=sr_1_1?ie=UTF8&s=books&qid=1287586055&sr=1-1
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选