为什么 bool 有时被称为 "flags"?

Why are bools are sometimes referred to as "flags"?

为什么布尔值有时被称为 "flags"?这只是一个比喻还是有一定的历史原因?

旗帜是传达信息的古老方式。一面旗帜,如果我们不考虑降半旗,它只有两种状态——升起或不升起。例如,考虑一面白旗——升起它意味着投降。不举起它,默认状态,意味着你没有投降。

一个布尔变量,就像一个旗帜,只有两种状态 - truefalse

Flag既可作名词也可作动词:To flag 可以表示注意、标记、发出信号(也许这是源于对航海旗的使用?)

可以在 1959 年的 IBM 1620 中找到计算机历史上对术语标志的早期(但可能不是第一次)使用(我的重点):

Memory was accessed two decimal digits at the same time (even-odd digit pair for numeric data or one alphameric character for text data). Each decimal digit was 6 bits, composed of an odd parity Check bit, a Flag bit, and four BCD bits for the value of the digit in the following format:

C F 8 4 2 1

The Flag bit had several uses:

  • In the least significant digit it was set to indicate a negative number (signed magnitude).
  • It was set to mark the most significant digit of a number (wordmark).
  • In the least significant digit of 5-digit addresses it was set for indirect addressing (an option on the 1620 I, standard on the 1620 II). Multi-level indirection could be used (you could even put the machine in an infinite indirect addressing loop). In the middle 3 digits of 5-digit addresses (on the 1620 II) they were set to select one of 7 index registers.

所以用于标记或指示某物的位称为标志位。

当然在flag fields或状态寄存器中使用"flag"是很自然的。

但是一旦标志和位之间的关联建立起来,它们的使用就可以互换也是可以理解的。当然这也适用于布尔变量。

PS: 同样的问题已经 asked,但不幸的是没有答案。