在原始字节数组中将所有位设置为1 [英] Set all bits to 1 in an array of raw bytes
问题描述
我有一个数组如下:
char无符号数据[N];
我想要将数组中的每一位设置为1.我最初的想法
是:
memset(数据,UCHAR_MAX,sizeof数据);
但是当我查看memset的声明时,我看到
middle参数是一个int而不是一个unsigned char。问题
这里是UCHAR_MAX并不保证适合在int。
任何人都可以想到一种方法将所有位设置为1(除了通过元素循环
并为其分配UCHAR_MAX) ?
可能,我需要的是一个int值,它会在每次实现时转换为UCHAR_MAX
......嗯......
马丁
I have an array as follows:
char unsigned data[N];
I want to set every single bit in the array to 1. My initial thoughts
were:
memset(data, UCHAR_MAX, sizeof data);
but then when I looked at the declaration for memset, I saw that the
middle parameter was an int rather than an unsigned char. The problem
here is that UCHAR_MAX isn''t guaranteed to "fit" in an int.
Can anyone think of a way to set all the bits to 1 (other than looping
through the elements and assigning UCHAR_MAX to them)?
Possibly, what I need is an int value that will convert to UCHAR_MAX
on every implementation... hmm...
Martin
推荐答案
9月27日下午3:15,Martin Wells< war ... @ eircom.netwrote:
On Sep 27, 3:15 pm, Martin Wells <war...@eircom.netwrote:
我有一个数组如下:
char无符号数据[N];
我想将数组中的每一位设置为1.我最初的想法
是:
memset(数据) ,UCHAR_MAX,sizeof数据);
但是当我查看memset的声明时,我看到
中间参数是一个int而不是一个无符号的字符。问题
这里是UCHAR_MAX并不保证适合在int。
任何人都可以想到一种方法将所有位设置为1(除了通过元素循环
并为其分配UCHAR_MAX) ?
可能,我需要的是一个int值,它会在每次实现时转换为UCHAR_MAX
......嗯......
Martin
I have an array as follows:
char unsigned data[N];
I want to set every single bit in the array to 1. My initial thoughts
were:
memset(data, UCHAR_MAX, sizeof data);
but then when I looked at the declaration for memset, I saw that the
middle parameter was an int rather than an unsigned char. The problem
here is that UCHAR_MAX isn''t guaranteed to "fit" in an int.
Can anyone think of a way to set all the bits to 1 (other than looping
through the elements and assigning UCHAR_MAX to them)?
Possibly, what I need is an int value that will convert to UCHAR_MAX
on every implementation... hmm...
Martin
如果你有2'的补充你可以传-1,这就是
1.
但是有人说有架构,它不是2'的补充(为什么,这很奇怪,他们找到了更好的编码方式
数字?)。
If you got 2''s complementary you can pass -1 which is all the bit to
1.
But some says that there is architecture where it''s not a 2''s
complementary (why, it''s strange, have they find a better way to code
the numbers ?).
如果你有2'的补充,你可以传-1,这是所有的一点
1.
但有些人说有架构,它不是一个2''s
互补(为什么,这很奇怪,他们找到了更好的编码方式
这些数字? )。
If you got 2''s complementary you can pass -1 which is all the bit to
1.
But some says that there is architecture where it''s not a 2''s
complementary (why, it''s strange, have they find a better way to code
the numbers ?).
关于C89,请完全便携。 。 。
Martin
Fully-portably please as regards C89. . .
Martin
Martin Wells写道:
Martin Wells wrote:
我有一个数组如下:
char无符号数据[N];
我想设置每一位数组为1.我最初的想法
是:
memset(数据,UCHAR_MAX,sizeof数据);
I have an array as follows:
char unsigned data[N];
I want to set every single bit in the array to 1. My initial thoughts
were:
memset(data, UCHAR_MAX, sizeof data);
怎么样?
memset(数据,~0,尺寸数据);
How about this?
memset(data,~0,sizeof data);
这篇关于在原始字节数组中将所有位设置为1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!