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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Ask HN: Why some languages use 1 byte for boolean type

16 点作者 Genius_um2 个月前
Some programming languages like D use 8 bits for their boolean type, why they don't use 1 bit ?

12 条评论

tlb2 个月前
C and C++ also use 8 bits in most cases. One reason is to support pointers, ie `bool *`.<p>You can get a single-bit bools in a C++ struct, with eg<p><pre><code> struct foo { bool a:1; bool b:1; ... } </code></pre> but you can&#x27;t take a pointer to such a member.
评论 #43422025 未加载
评论 #43422110 未加载
评论 #43422977 未加载
评论 #43422126 未加载
fatuna2 个月前
Because engineering is all about making choices. Making a boolean 1 bit would be space efficient. However, memory is being read at least 1 byte at a time. If you want 1 bit of that byte, that&#x27;s an extra instruction.<p>So storing a boolean in one byte is more speed efficient!<p>(In C you can store a boolean in one bit. If, for example, you need to store a great number of booleans and memory size is more important than speed. )
Veliladon2 个月前
I think the better question is why use a single bit or byte for a single bool when sum types exist?<p>You&#x27;re pulling in an entire cache line (64 bytes) with any load. Why would you not just turn the bool into sum type that carries the payload with them? That way you can actually use the rest of the cache line instead of loading 64 bytes to work with a single bit, throwing the other 511 bits in the trash, and then doing another load on top for the data.<p>It&#x27;s even worse when you do multithreading with packed bools because threads can keep trashing each other&#x27;s cache lines forcing them to wait for the load from L3, or worse, DRAM.
评论 #43422476 未加载
bjourne2 个月前
Unless you&#x27;re dealing with packed data (i.e., long arrays), there is no point in having scalars be less than 32 bits wide on modern architectures. This is due to architectures being optimized for 32 or 64 bit wide registers and even wider data buses. Writing a single byte to memory is in fact more expensive than writing 64 bytes.
mschuster912 个月前
The minimum access in modern computers is 1 byte of 8 bits, which means that to losslessly change the value of an 1-bit bool you need three instructions:<p>1. load byte at $address into $register<p>2. use whatever native instructions there are to change just that single bit in $register - in the worst case you need multiple of them<p>3. write byte from $register into $address<p>In contrast, all modern platforms have a single &quot;store immediate (=hardcoded in the bytecode) value&quot; instruction, so it&#x27;s either two (set register to 0 &#x2F; 1, write register to RAM) or even one (write value directly to RAM) instruction.<p>Bit-packing structures (another poster showed an example here in the thread) used to be done pretty much everywhere and you&#x27;ll notice it if you deal with reverse engineering code even from the Windows 98 era... but in anything more modern it&#x27;s not needed because computers nowadays have more than 640 KB of RAM [1].<p>By the way, accessing &#x2F; modifying small values in large(r) containers is a common problem in computing in general, so if it interests you, you might want to look into &quot;SSD write amplification&quot;, &quot;SMR HDD write amplification&quot; or &quot;flash wear leveling&quot;.<p>[1] <a href="https:&#x2F;&#x2F;lunduke.locals.com&#x2F;post&#x2F;5488507&#x2F;myth-bill-gates-said-640k-ought-to-be-enough-for-anybody" rel="nofollow">https:&#x2F;&#x2F;lunduke.locals.com&#x2F;post&#x2F;5488507&#x2F;myth-bill-gates-said...</a>
PaulHoule2 个月前
It would be slower to get or set one bit in a byte than to treat the whole byte as a bit. Pipelining could hide the read overhead but memory operations are slow and setting one bit would be mean reading the byte, doing an AND or OR and then writing the byte back.
评论 #43422022 未加载
Fell2 个月前
Most video games compress booleans to bitfields in the save files to save space. Depending on the game, a save file can contain thousands of objects, so it makes sense to keep it small.<p>At runtime, booleans are 1 byte each. The additional work of shifting and masking to get to the bit you want simply isn&#x27;t worth it. When reading from memory, the CPU will read a whole cache lines of 64 bytes anyways. So unless you want to process a lot of booleans (and only booleans) at once, it&#x27;s simply not worth having them 1 bit each.<p>Because aligned data runs faster, a single boolean sandwiched inbetween integers or pointers can even take up 4 or 8 bytes of memory.<p>Note: Some languages have built-in optimisations where an array of booleans is actually 1 bit each. C++&#x27;s `std::vector&lt;bool&gt;` is such a case and I&#x27;m sure there are more.<p>I&#x27;ve seen people advise to never use booleans, simply because a number (or enum) can convey more meaning while taking up the same amount of space.
评论 #43425902 未加载
zamalek2 个月前
&gt; Some programming languages<p>Other answers go into detail about why, but you have a more fundamental misconception: nearly all use at least 1 byte, the majority of which use 4 bytes.<p>Using 4 bytes is partially historical, x86 used to suffer from several performance pitfalls if pointer accesses weren&#x27;t aligned (multiples of 4). While his generally isn&#x27;t an issue on x86 any more, other architectures do still have this limitation. ABIs designed during that era (i.e. many of the ones we use today) inherited that limitation out of nessecity. Furthermore some instructions (including on recent x86 designs, I think) need to be 4 byte aligned.
AnyTimeTraveler2 个月前
Take a look at the Intel 8052 Microcontroller. It has a part of its internal RAM that can be addressed as bytes with normal instructions, but also has special bit-instructions. You could set and clear a bit and jump if is was set or not set. I used this feature a lot when making a little tetris game for the processor, but it was not essential.
wruza2 个月前
I hate to be that guy, but seriously, googling $subj yields thousands of fully detailed answers right away. This question is from the &quot;answered countless times&quot; category. Recognizing this may be hard though when you start, so next time try giving web search a chance, cause it may save lots of time.
评论 #43423117 未加载
mytailorisrich2 个月前
As hinted by another commenter this is because a byte is usually the smallest unit addressable in memory so using a whole byte even if the type does not require it simplifies everything.
评论 #43422065 未加载
cmrdporcupine2 个月前
As others have said: The way CPUs fetch and access memory is in larger word sizes. Even 1 8-bit byte can be inefficient depending on how that byte aligns in the larger structure and which ISA you&#x27;re talking about.<p>Memory is cheap, so. <i>shrug</i><p>In embedded this can be a very different story though. There we are often working with tiny memories and lower clock speeds and the concern is packing everything in tighter.