如果最高有效字节的最高有效位为1,则样本为负

时间:2020-09-30 04:20:16

标签: c++ audio wav

嗨,我正在做一个音频处理项目

代码工作正常,但我不明白这三行内容

如果有人知道这些代码行怎么说

// Otherwise, if the most-significant bit of most-significant byte is 1, then
// sample is negative, so we need to set the upper bytes to all 1s.
else if (sampleBytes[bytesPerSample-1] & 0x80)
{
    for (size_t b = bytesPerSample; b < sizeof(sample); b++)
    {
        sampleBytes[b] = 0xFF;
    }
}

1 个答案:

答案 0 :(得分:0)

这些行通过将位于“ sampleBytes”中的“ bytesPerSample”字节扩展为“ sample”变量的字节数(如果为负,则用“ 1”完成)。

在大多数系统中,负数是二进制形式的两种补码形式(https://en.wikipedia.org/wiki/Two%27s_complement)。也就是说,-x = NOT(x)+ 1。

For example:
11111111 = 1
11111110 = 2
11111101 = 3
11111100 = 4
...

因此,如果数字为负数,则必须用一个数字扩展以保持该值。

第一行断言数字是否为负,第二行迭代多余的字节,最后一行将其放入十六进制。

相关问题