什么是“位填充"?或“填充位"确切地? [英] What is "bit padding" or "padding bits" exactly?

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

问题描述

我不想用这个来骚扰你,但我在互联网上的任何地方都找不到对什么是位填充"的详细解释.确实如此,并且在 StackOverflow 上与位填充相关的线程的任何答案中都没有.

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 中的位填充".已被提及,但在我需要时并未对其进行解释.

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 上查看填充"的相关标签时,填充被描述为:

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

插入内存结构的额外空间以实现地址对齐 - 或 - 框架和 HTML 元素内容之间的额外空间 - 或 - 使用格式化打印命令(如 C 中的 printf )打印值时的额外空间或零*-系列函数.

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.

推荐答案

我经常发现术语位填充"与数据类型相关,但不了解它是什么,也不了解它对这些类型的确切作用.

I often find the term "bit padding" in relation of data types, but don´t understand what it is nor what it does exactly 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 通常不会再次存储在两个不同的桶中.为了解决这个问题,编译器在 cd 之间添加了填充位,比如

Now bar is 40 bits wide and d more often then not will be stored in two different buckets 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天全站免登陆