比特字节序如何影响使用C按位的变化和文件IO? [英] How bit endianness affects bitwise shifts and file IO in C?

查看:120
本文介绍了比特字节序如何影响使用C按位的变化和文件IO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是两台机器。从责令其的的LSB
(最低有效位)到MSB(最高有效位),而为了
从MSB到LSB。或者,换句话说,使用小端而
使用大端的的 - 不要与混淆字节 - 排序

Let L and B be two machines. L order its bits from LSB (Least Significant Bit) to MSB (Most Significant Bit) while B order from MSB to LSB. Or, in other words, L uses Little Endian while B uses Big Endian bit - not to be confused with byte - ordering.

问题1 解决

我们正在编写以下code,我们想成为便携式:

We are writing the following code which we want to be portable:

#include <stdio.h>

int main()
{
    unsigned char a = 1;
    a <<= 1;

    printf("a = %d\n", (int) a);

    return 0;
}

后,将打印2,但在会发生什么?它会转移
1淘汰和打印0?

on L, it will print 2, but what happens on B? Will it shift the 1 out and print 0?.

解决方案:是在6.5.7 C99定义,说它是,至少在
无符号整型,&LT;&LT; &GT;&GT; 将成倍增加,并除以2
分别。

SOLUTION: The C99 definition at 6.5.7 says it that, at least on unsigned integer types, << and >> will multiply and divide by 2 respectively.

问题2:

我们正在编写以下code,我们想成为便携式:

We are writing the following code which we want to be portable:

读程序:

/* program READ */
#include <stdio.h>

int main()
{
    FILE* fp;
    unsigned char a;

    fp = fopen("data.dat", "rb");
    fread(&a, 1, 1, fp);
    fclose(fp);

    return 0;
}

和写程序:

/* program WRITE */
#include <stdio.h>

int main()
{
    FILE* fp;
    unsigned char a = 1;

    fp = fopen("data.dat", "wb");
    fwrite(&a, 1, 1, fp);
    fclose(fp);

    return 0;
}

如果我们运行在后,将数据文件移动到WRITE会发生什么的
运行阅读呢?如果我们运行写的,然后在阅读

what happens if we run WRITE on L, move the data file to B and run READ there? And if we run WRITE on B and then READ on L?

很抱歉,如果这是一个常见问题。我GOOGLE了几个小时没有运气。

Sorry if this is a FAQ. I googled for hours without luck.

推荐答案

位字节顺序不影响存储在磁盘中的字节的数据。字节字节序会。

Bit Endianness doesn't affect data stored on disks in bytes. Byte Endianness will.

位字节序的东西,对于其中字节发送一个位在一个时间串行接口事项,以及发送者和接收者需要的字节顺序达成一致。例如,位顺序在 SPI 设备各不相同,你需要尝试从设备读取之前引用的数据表。

Bit Endianness is something that matters for serial interfaces where a byte is sent one bit at a time, and the sender and receiver need to agree on the byte order. For example, bit order in SPI devices varies and you need to reference the data sheet before attempting to read from the device.

下面就是维基百科对位字节序说:

条款位或字节顺序位级
  字节序很少使用时
  谈论一个重新presentation
  储值,因为它们仅
  有意义的罕见计算机
  体系结构,其中每个
  位具有唯一的地址。他们是
  然而使用来指
  在一个比特的传输顺序
  串行网上平台。最常见的顺序
  是透明的管理
  硬件,并且是位级模拟
  的小端(低位在前),
  尽管存在协议需要
  相反的顺序(例如I²C)。在
  联网,有关的决定
  比特的传送顺序是由
  在数据链路的最底部
  层OSI模型的。

The terms bit endianness or bit-level endianness are seldom used when talking about the representation of a stored value, as they are only meaningful for the rare computer architectures where each individual bit has a unique address. They are used however to refer to the transmission order of bits over a serial medium. Most often that order is transparently managed by the hardware and is the bit-level analogue of little-endian (low-bit first), although protocols exist which require the opposite ordering (e.g. I²C). In networking, the decision about the order of transmission of bits is made in the very bottom of the data link layer of the OSI model.

在你的情况下,物理硬盘接口定义了位顺序,不管是怎么回事读取或写入它的处理器。

In your case, the physical hard drive interface defines the bit order, regardless of the processor that's going to read or write it.

这篇关于比特字节序如何影响使用C按位的变化和文件IO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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