一个字节可以大于8位? [英] A byte can be greater than 8 bits?

查看:130
本文介绍了一个字节可以大于8位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我读到它时,C99声明一个字节是:


可寻址的数据存储单元,足以容纳

的任何成员基本字符

设置的执行环境 (3.6)


并且一个字节必须至少为8位:


"下面给出的值应该用常量表达式替换

适合在#if

预处理指令中使用。此外,除了CHAR_BIT和

MB_LEN_MAX之外,

以下内容将被替换为类型相同的表达式



表达式是相应类型的对象转换

根据整数

促销。它们的实现定义值应相等或者大于

(绝对值)与显示的值相同,具有相同的符号。

最小对象的位数不是位字段(字节)

CHAR_BIT 8(5.2.4.2.1)


这是否意味着一个字节可以大于8位(即CHAR_BIT>

8)?我得到的印象是一个字节或一个unsigned char,总是8位,但也许我错了。如果我不是,那么在标准中某处定义一个字节总是8位是否有



问候,

B.

As I read it, C99 states that a byte is an:

"addressable unit of data storage large enough to hold any member of
the basic character
set of the execution environment" (3.6)

and that a byte must be at least 8 bits:

"The values given below shall be replaced by constant expressions
suitable for use in #if
preprocessing directives. Moreover, except for CHAR_BIT and
MB_LEN_MAX, the
following shall be replaced by expressions that have the same type as
would an
expression that is an object of the corresponding type converted
according to the integer
promotions. Their implementation-defined values shall be equal or
greater in magnitude
(absolute value) to those shown, with the same sign."

number of bits for smallest object that is not a bit-field (byte)
CHAR_BIT 8 (5.2.4.2.1)

Does this mean that a byte can be larger than 8 bits (ie CHAR_BIT >
8)? I have gotten the impression that a byte, or unsigned char, was
always 8 bits, but perhaps I was wrong. If I am not, is there
somewhere in the standard that defines a byte as always being 8 bits?

Regards,
B.

推荐答案

bo *******@gmail.com 写道:

>当我读到它时,C99声明一个字节是:
可寻址的数据存储单元,其大小足以容纳执行环境的基本字符集的任何成员。 (3.6)

并且一个字节必须至少为8位:
...
这是否意味着一个字节可以大于8位(即CHAR_BIT>
8)?我得到的印象是一个字节或无符号字符总是8位,但也许我错了。
>As I read it, C99 states that a byte is an:
"addressable unit of data storage large enough to hold any member of
the basic character set of the execution environment" (3.6)

and that a byte must be at least 8 bits:
...
Does this mean that a byte can be larger than 8 bits (ie CHAR_BIT >
8)? I have gotten the impression that a byte, or unsigned char, was
always 8 bits, but perhaps I was wrong.



正确 - 例如,用于DSP 2000的Texas Instruments C编译器
处理器系列定义CHAR_BIT == 16和sizeof(char) ==

sizeof(短)== sizeof(int)== 1

-

Roberto Waltman

[请回复小组,

返回地址无效]

Correct - For example, Texas Instruments C compiler for the DSP 2000
processor family defines CHAR_BIT == 16, and sizeof(char) ==
sizeof(short) == sizeof(int) == 1
--
Roberto Waltman

[ Please reply to the group,
return address is invalid ]




我会非常感兴趣的海报在这里列出他们使用的当前系统

,这会导致问题违反规则。它可能是常见问题解答中的一个好的版本,供人们外出并找到真正的系统,以便更好地了解为什么他们需要小心:


Martin Wells< wa **** @ eircom.netwrites:

I would be very interested for posters here to list the current systems
they use which would cause problems by breaking the rules. It might be a
good edition to the FAQ for people to go out and find real systems so as
to understand better why they need to be careful:

Martin Wells <wa****@eircom.netwrites:

>这是意味着一个字节可以大于8位(即CHAR_BIT>
8)?
>Does this mean that a byte can be larger than 8 bits (ie CHAR_BIT >
8)?




是的,一个字节可以有超过8位。欢迎来到世界

便携式C编程:D这里还有其他一些需要注意的事项

便携式编程:



Yes, a byte can have more than eight bits. Welcome to the world of
portable C programming :D Here''s a few other things to look out for in
portable programming:



A)字节超过8位的系统:

A) Systems where a byte is more than 8 bits :


>

1:空指针不是't必须表示为全位 - 零,所以

三思而后行使用memset(array_of_pointers,0,sizeof

array_of_pointers)。
>
1: Null pointers aren''t necessarily represented as all-bits-zero, so
think twice about using memset(array_of_pointers,0,sizeof
array_of_pointers).



B)无法通过应用0来设置NULL指针的系统。

B) Systems where a NULL pointer can not be set by applying 0s.


>

2:除了unsigned char之外的整数类型可能包含填充位,因此

远离memcmp(arr1,arr2,sizeof arr1)。
>
2: Integer types other than unsigned char may contain padding bits, so
stay away from memcmp(arr1,arr2,sizeof arr1).



C)填充位不一致地设置在相同类型的2

数组之间的位置。

C) Places where the padding bits are not concistently set between 2
arrays of the same types.


>

3:可以使用除两个补码以外的数字系统,所以请小心使用/ b $ b -1代表所有位 - 一。
>
3: Number systems other than two''s complement may be used, so be
careful about doing things like using -1 to represent all-bits-one.



D)其中-1不是所有位。

D) Where -1 is not an all bits on.


>

4:函数指针可能不适合任何其他类型(例如

,如void *或unsigned long),所以不要这样做。
>
4: Function pointers might not fit inside ANY of the other types (e.g.
such as void* or unsigned long), so don''t do that.



E)函数指针不能存储在VOID

指针中。 (无论风格如何)

E) Where a function pointer can not be stored in a VOID
pointer. (Regardless of style)


Richard写道:
Richard wrote:

我对海报非常感兴趣这里列出他们使用的当前系统

,这会导致违反规则的问题。它可能是常见问题解答中的一个好的版本,供人们外出并找到真正的系统,以便更好地了解为什么他们需要小心:
I would be very interested for posters here to list the current systems
they use which would cause problems by breaking the rules. It might be a
good edition to the FAQ for people to go out and find real systems so as
to understand better why they need to be careful:



我部分同意,部分不同意。我同意,因为它向新的

人显示严格遵循标准/是/重要因为这些

常见假设并不一定正确。但我不同意,因为它可能会鼓励人们认为,如果找不到与常规不同的系统,那么我们是不是很重要做出假设。

I partly agree and partly disagree here. I agree because it shows to new
people that strictly following the standard /is/ important because these
common assumptions aren''t necessarily true. But I disagree because it
may encourage the view that if a system cannot be found which differs
from the norm, then it doesn''t matter that we''re making assumptions.


Martin Wells< wa **** @ eircom.netwrites:
Martin Wells <wa****@eircom.netwrites:

>>这是否意味着一个字节可以大于8位(即CHAR_BIT>
8)?
>>Does this mean that a byte can be larger than 8 bits (ie CHAR_BIT >
8)?


是的,一个字节可以有超过8位。欢迎来到便携式C编程世界:D这里有一些其他需要注意的东西便携式编程:


Yes, a byte can have more than eight bits. Welcome to the world of
portable C programming :D Here''s a few other things to look out for in
portable programming:



A )一个字节大于8位的系统:


A) Systems where a byte is more than 8 bits :



DSP就是一个常见的例子。

DSPs are a common example here.


> 1:空指针不一定表示为全位零,所以
考虑使用memset(array_of_pointers,0,sizeof
array_of_pointers)。
>1: Null pointers aren''t necessarily represented as all-bits-zero, so
think twice about using memset(array_of_pointers,0,sizeof
array_of_pointers).



B)无法通过应用0设置NULL指针的系统。


B) Systems where a NULL pointer can not be set by applying 0s.



你是什么意思申请0s?空指针始终可以通过以下方式设置:

foo * x =(foo *)0;

(我不确定是否需要演员表)即使null指针

表示不是全位零。


我个人不知道任何非全零位零指针系统,

但我从没见过理由担心它。无论如何,我知道/我的/代码将工作

无处不在。正如Kernighan和Ritchie明智地指出的那样,如果你不知道他们是如何在各种机器上完成的,那么无罪就可以帮助保护你。 '''

What do you mean by "applying 0s"? Null pointers can always be set by:
foo *x = (foo *)0;
(I''m not sure if the cast is necessary) even if the null pointer
representation is not all-bits-zero.

I personally don''t know of any non-all-bits-zero null pointer systems,
but I''ve never seen reason to worry about it. I know /my/ code will work
everywhere regardless. As Kernighan and Ritchie wisely point out, ``if
you don''t know how they are done on various machines, that innocence may
help to protect you.''''


> 2:除了unsigned char之外的整数类型可能包含填充位,所以
远离memcmp(arr1,arr2,sizeof arr1)。
>2: Integer types other than unsigned char may contain padding bits, so
stay away from memcmp(arr1,arr2,sizeof arr1).



C)填充位不一致地设置在相同类型的2

数组之间的位置。


C) Places where the padding bits are not concistently set between 2
arrays of the same types.



这取决于用户程序。任何带有填充位的系统都可以通过用户程序设置它们:
int main(void){

unsigned int arr1 [2] ,arr2 [2];

arr1 [0] = arr1 [1] =(unsigned int)-1;

memset(arr2,2 * sizeof(unsigned int) ,1,(unsigned char)-1);

/ *此时,arr1'的值等于arr2'的值但是

填充位可能不同。 * /

}

That is user-program dependent. Any system with padding bits can have
them set by the user program:
int main(void) {
unsigned int arr1[2], arr2[2];
arr1[0] = arr1[1] = (unsigned int) -1;
memset(arr2,2*sizeof(unsigned int),1,(unsigned char)-1);
/* At this point, arr1''s values equal arr2''s values but
the padding bits may differ. */
}


> 3:除了两个补码之外的数字系统可能是使用过,所以要小心做一些事情,比如使用-1表示所有比特一。
>3: Number systems other than two''s complement may be used, so be
careful about doing things like using -1 to represent all-bits-one.



D)其中-1不是全部位。


D) Where -1 is not an all bits on.



这又是一个复杂的。对于无符号类型,-1总是全位 - 一。

我想不出你为什么要将签名类型设置为all-bits-one。

This is compicated again. For unsigned types, -1 is always all-bits-one.
I can''t think why you''d want to set a signed type to all-bits-one.


> 4:函数指针可能不适合任何其他类型(例如
,如void *或unsigned long),所以不要这样做。
>4: Function pointers might not fit inside ANY of the other types (e.g.
such as void* or unsigned long), so don''t do that.



E)函数指针无法存储在VOID

指针中。 (无论风格)


E) Where a function pointer can not be stored in a VOID
pointer. (Regardless of style)



我想不出你为什么要这样做。


- -

Philip Potter pgp< atdoc.ic.ac.uk

I can''t think why you''d want to do this.

--
Philip Potter pgp <atdoc.ic.ac.uk


这篇关于一个字节可以大于8位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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