什么是“位填充”?或“填充位”究竟? [英] What is "bit padding" or "padding bits" exactly?

查看:600
本文介绍了什么是“位填充”?或“填充位”究竟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想骚扰您,但是我只是无法在互联网上的任何地方找到有关比特填充的详细说明。

I do not want to molest you with this, but i just can not find anywhere in the internet a well-described explanation for what "bit padding" really is, as well as not in any answer for bit padding-related threads here on StackOverflow.

我也在ISO 9899-1990中搜索了 bit padding。

I also searched ISO 9899-1990 for it, in which "bit padding" is refered to but quite not explained as i need it.

我在网络上发现的唯一与此相关的内容是此处,其中只对一个句子给出了一个荒谬的简短解释,说:

The only content in the web i found about this was here, where only one ridiculously short explanation of one sentence was given, saying:


位填充:


位填充是向传输或存储单元添加一个或多个额外的位,以使其符合标准大小。

bit padding:

Bit padding is the addition of one or more extra bits to a transmission or storage unit to make it conform to a standard size.

某些资料来源将位填充标识为位填充

Some sources identify bit padding as a type of bit stuffing.

至少提供了某种信息,但对我来说还不够。我不太明白那是什么意思。它也指术语
位填充

Which it at least some sort of information but not enough explanation for me. I don´t quite understand what that means exactly. It also refers to the term "bit stuffing".

当我在StockOverflow上查看 padding ,填充描述为:

When i look at the relative tag here on StockOverflow for "padding", padding is described as:


插入内存结构以实现地址对齐的多余空间-或HTML元素的内容-使用格式打印命令(如C)中的函数printf * -family格式化输出值时,-或-多余的空格或零。

Extra space inserted into memory structures to achieve address alignment -or- extra space between the frame and the content of an HTML element -or- extra spaces or zeros when printing out values using formatting print commands like, in C, the printf*-family of functions.


背景:


我经常发现术语位填充关于数据类型,但不了解它是什么,也不了解这些数据是什么。

Background:

I often find the term "bit padding" in relation of data types, but don´t understand what it is nor what it does exaclty with those.

非常感谢您提供任何基于主题的答案。

Thank you very much for any topic-based answer.

推荐答案


我经常在术语 bit padding中使用 b。

I often find the term "bit padding" in relation of data types, but don´t understand what it is nor what it does exaclty with those.

其要点在于它们是。浪费空间。我说的是浪费因为具有填充位可以使对象变大,但可以使对象的工作更加轻松(这意味着更快),而空间浪费也可以带来巨大的性能提升。在某些情况下,这是至关重要的,因为CPU无法处理该大小的对象。

The gist of it is they are "wasted" space. I say "wasted" because while having padding bits makes the object bigger, it can make working with the object much easier (which means faster) and the small space waste can generate huge performance gains. In some cases it is essential because the CPU can't handle working with objects of that size.

让我们说您有一个类似的结构(所有数字仅是示例,不同的平台可以具有

Lets say you have a struct like (all numbers are just an example, different platforms can have different values):

struct foo
{
    short a; // 16 bits
    char  b; // 8 bits 
};

,您正在使用的计算机在一次读取操作中读取32位数据。读取单个foo并不是问题,因为整个对象都适合该32位块。确实有问题的是当您拥有数组时。关于数组要记住的重要一点是它们是连续的,元素之间没有空格。它只是一个对象,紧随其后。因此,如果您有一个类似

and the machine you are working with reads 32 bits of data in a single read operation. Reading a single foo is not a problem since the entire object fits into that 32 bit chunk. What does become a problem is when you have an array. The important thing to remember about arrays is that they are contiguous, there is no space between elements. It's just one object immediately followed by another. So, if you have an array like

foo array[10]{};

第一个 foo 对象位于32位存储桶中。不过,数组的下一个元素将位于第一个32位存储桶和第二个32位存储桶中。这意味着成员 a 在两个单独的存储桶中。如果您尝试执行某些操作,则某些处理器可以(收费)执行此操作,而其他处理器则会崩溃。为了解决这两个问题,编译器将在 foo 的末尾添加填充位以填充其大小。这意味着foo实际上变成了

With this the first foo object is in a 32 bit bucket. The next element of the array though will be in the first 32 bit bucket and the second 32 bit bucket. This means that the member a is in two separate buckets. Some processors can do this (at a cost) and other processors will just crash if you try to do this. To solve both those problems the compiler will add padding bits to the end of foo to pad out it's size. This means foo actually becomes

struct foo
{
    short a; // 16 bits
    char  b; // 8 bits 
    char  _; // 8 bits of padding
};

现在,处理器很容易处理 foo 对象单独或以阵列形式显示。它不需要做任何额外的工作,并且每个对象仅添加了8位。您可能需要很多对象才能在现代计算机上开始发挥作用。

And now it is easy for the processor to handle foo objects by themselves or in an array. It doesn't need to do any extra work and you've only added 8 bits per object. You'd need a lot of objects for that to start to matter on a modern machine.

有时,由于访问方式不统一,您需要在类型成员之间进行填充。假设您有

There is also times where you need padding between members of the type because of unaligned access. Lets say you have

struct bar
{
    char c; // 8 bits
    int  d; // 32 bits
};

现在 bar 的宽度为40位,而 d 则不会再存储在两个不同的位置。为了解决这个问题,编译器在 c d 之间添加填充位,例如

Now bar is 40 bits wide and d more often then not will be stored in two different again. To fix this the compiler adds padding bits between c an d like

struct bar
{
    char    c; // 8 bits
    char _[3]; // 24 bits
    int     d; // 32 bits
};

现在保证 d 可以进入一个32位桶。

and now d is guaranteed to go into a single 32 bit bucket.

这篇关于什么是“位填充”?或“填充位”究竟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆