TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Understanding the power of bitwise operators

138 点作者 Decabytes大约 2 年前

18 条评论

WaitWaitWha大约 2 年前
I like introducing people to new concepts, and I will assume this entry is for non-technologists. This is an excellent start.<p>If I may suggest, stop insulting your readers (e.g., several places implying reader does or does not do something &#x27;normally&#x27;, or not know something).<p>In cases where an error is well known, describe the error so it is understood you are using it as an example, and not an actual error (e.g., Null character (�) a black diamond with white question mark in the middle).<p>Single bit is the most often used type of data (vs &quot;single bit is almost completely useless&quot;). Your lights are on, or off; you are dead or alive; it is today or not, vast majority of network flags in various protocols are single bits.<p>There are a lot of weasel words throughout the post. If you plan to educate, uncertainty reduces comprehension and retention. There are some absolute statements are made too, but might rightfully questioned (e.g., [b]inary is the only language computers understand). Make sure your absolutes are truly absolutes.<p>Keep up the good work.
评论 #35905728 未加载
dragontamer大约 2 年前
Bitwise operators are the beginners introduction to SIMD programming.<p>A 64 bit register doing XOR is really 64x parallel XOR happening in parallel each of size 1 bit.<p>A shift, rotate are just fancy movement operators and exist in the SIMD space.<p>Good bitwise programming IMO requires a full embrace of this parallel mindset. See the Chess Programming Wiki for all sorts of things that 64x parallel bits can represent.<p>In particular, a 8x8 chessboard maps well to 64 bits.<p>-------<p>No really. Do remember that one of the OG SIMD computers, the CM2 (connection machine 2) was a 1-bit core but SIMD across thousands of cores (bits). And a lot of research and understanding of modern parallel concepts comes from the Connection Machine.
akhayam大约 2 年前
For folks that are interested in learning more about bitwise operations, here is another article (slightly more nuanced) about XORs that you may enjoy reading:<p>All About XOR: <a href="https:&#x2F;&#x2F;accu.org&#x2F;journals&#x2F;overload&#x2F;20&#x2F;109&#x2F;lewin_1915&#x2F;" rel="nofollow">https:&#x2F;&#x2F;accu.org&#x2F;journals&#x2F;overload&#x2F;20&#x2F;109&#x2F;lewin_1915&#x2F;</a>
评论 #35917946 未加载
JonChesterfield大约 2 年前
Really weird to start with &#x27;that one section that most of us skip&#x27; and then use an _instruction set emulator_ as the real world example. Who exactly is both frightened of the &amp; symbol and wants to learn about instruction encoding?
评论 #35907862 未加载
DubiousPusher大约 2 年前
If you like this, I recommend Hacker&#x27;s Delight. The first chapter goes into bit twiddling in depth. It ends with some explanation of what all can be expressed in bit-wise operations, a sort of Fundamental Theorem of Bit Twiddling.
JKCalhoun大约 2 年前
I was just doing some bit shifting yesterday. I wanted to pass a drawing function a Uint16 that would represent a 4x4 black and white pixel pattern. It was a little hack to create early MacOS-like pixel patterns in SDL. SDL has primitives for line and rectangle drawing with solid colors but little else.<p>So I wrote some straight C, some nested for loops for row&#x2F;column of the area to be <i>patterned</i>. I used row % 4, column % 4 to map to the appropriate bit within the Uint16 &quot;pattern&quot;. Some bit shifting, masking and I knew whether to fill the pixel with foreground or background colors.<p>I always enjoy bitwise operations. (There&#x27;s I guess the classic programmer problem of swapping the values of two numbers in two registers in place.)
评论 #35908935 未加载
amflare大约 2 年前
&gt; While at first, they might seem obscure, unhelpful, or tools for people who write in low level programming languages, they do serve a purpose.<p>Firstly, no one says that bitwise operators don&#x27;t serve a purpose. Secondly, this is an interesting way to start an article that then goes on to explain how useful bitwise operators are for low level programming.
bvinc大约 2 年前
&gt; right shifting adds zeros to the most significant side. So, using the same number we get…<p>1110 &gt;&gt; 4 -&gt; 0000 1110
评论 #35906522 未加载
评论 #35906390 未加载
pattrn大约 2 年前
One of my earliest exposures to the power of bitwise operators happened when I was learning to write chess engines. This was right around the time 64 bit processors were hitting the markets, which allowed storing one bit of information per square, making it ideal for efficient processing via bitwise operators. There were some clever operations that allowed finding sliding piece collisions in only a few assembly instructions (with prodigious use of BSR and BSF) and a small amount of precomputed memory.<p>For anyone interested in binary chess math:<p><a href="https:&#x2F;&#x2F;www.chessprogramming.org&#x2F;Bitboards#General_Bitboard_Techniques" rel="nofollow">https:&#x2F;&#x2F;www.chessprogramming.org&#x2F;Bitboards#General_Bitboard_...</a>
photochemsyn大约 2 年前
There&#x27;s a fairly famous bit twiddling hacks site hosted at Stanford but I find this interpretation is more accessible, and has a better breakdown, and more accessible code examples. Here&#x27;s the page that&#x27;s all about using the bitwise operations to query the status of the kth bit in a binary sequence:<p><a href="https:&#x2F;&#x2F;www.techiedelight.com&#x2F;bit-hacks-part-2-playing-kth-bit&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.techiedelight.com&#x2F;bit-hacks-part-2-playing-kth-b...</a><p>Incidentally, deciphering some unknown bitwise expression is a task that ChatGPT excels at. For example, try asking it to do a stepwise breakdown and summary of:<p>unsigned int a, b, mask, r;<p>r = a ^ ((a ^ b) &amp; mask);
zabzonk大约 2 年前
i would recommend using &quot;raised to the power of&quot; (or similar) in place of &quot;^&quot; earlier on in the article, as it may get confused with the xor later on.<p>and use fixed fonts for all the numbers.
tzot大约 2 年前
Is it an autocorrect issue or the writer only-heard-never-read that the &quot;^&quot; character is called a “carrot”?
评论 #35906079 未加载
graypegg大约 2 年前
Was hoping for some magic at the end! Bit masking is one thing, but Bloom filters [0] are one of the coolest things to use when explaining why XOR is all over the place in computer science. It&#x27;s behaviour is just unexpected and surprising enough to be a fun puzzle, but not too complex to make explaining it need more than 3 min and a white board.<p>[0] <a href="https:&#x2F;&#x2F;llimllib.github.io&#x2F;bloomfilter-tutorial&#x2F;" rel="nofollow">https:&#x2F;&#x2F;llimllib.github.io&#x2F;bloomfilter-tutorial&#x2F;</a>
评论 #35909508 未加载
czscout大约 2 年前
Who is this article for exactly? It starts off by trying to relate to the reader by presenting the point about learning a new programming language, but then goes on to explain one of the most fundamental concepts of computing, as if the reader is a complete novice. I would imagine that nearly every person with programming experience, whether that be formal or not, would have at least some familiarity with binary representation and bitwise operations.
评论 #35907615 未加载
评论 #35912882 未加载
评论 #35908303 未加载
runlaszlorun大约 2 年前
OT but his mention of the calculator reminded me of discovering just yesterday that the built in Mac calculator has a Programmer mode (apparently Windows too) for hex, etc.<p>I’d been using the RPN&#x2F;scientific mode for ever but never new about Programmer mode.<p>I’m actually doing a fair amount of hex conversions and bit fiddling these days (webassembly bytecode) so it was actually something useful.
评论 #35908417 未加载
vijaybritto大约 2 年前
Can someone please explain why malloc(7) allocates only 2 extra bytes in the first slider? It has more than 7 bytes available but only 2 are allocated.
drivers99大约 2 年前
&gt; &quot;Typical of apple to hide this useful feature from us&quot;<p>It&#x27;s right there in Calculator&#x27;s menu: View -&gt; Programmer
firatsarlar大约 2 年前
It is art. Lovely. In commercial world, it is for low level people -- think --: Hope they do all magic for us :)