What is meant by “both bits” when using bitwise operators

断了今生、忘了曾经 提交于 2019-12-24 08:51:58

问题


This W3Schools page describes bitwise AND as:

Sets each bit to 1 if both bits are 1.

And an example is given:

5 & 1 returns 1, same as 0101 & 0001 returns 1.

As far as I know, 0101 are 4 bits and 0001 are another 4 bits. So, what is meant by "if both bits are 1"? What are these bits they are talking about?


回答1:


A bitwise operation is exactly bitwise, each bit goes into the CPU and gets combined with another bit.

So 0001 AND 0101, each of the four output bits are calculated according to that formula. If bit on position 1 in the left operand AND bit on position 1 in the right operand are both 1, the output gets the bit set on position 1.

A stronger CPU can calculate many of these operations in parallel, a 64-bit CPU can do 64 bits in parallel etc. In your example, 4 bits as to be calculated in parallel.

(Then these operations are combined in the CPU to du other arithmetic operations in the ALU, latching databits in a TCM etc in different parts of the CPU.)



来源:https://stackoverflow.com/questions/50787375/what-is-meant-by-both-bits-when-using-bitwise-operators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!