测试一下 [英] Test a bit

查看:59
本文介绍了测试一下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!


我是C的新手,抱歉这两个问题...


首先,我想知道哪个命令允许我从char变量中测试一下




其次,我想编写一个代码检查程序。我的代码是36位长,

但是我不能做一个位数组或指针(我的编译器不接受它)。我怎么建立一个?b $ b?你能给我一些帮助我的命令吗?


非常感谢!


Sam

解决方案

" Sam(rép.sans-no-sp-am)"写道:


大家好!

我是C的新手,抱歉这两个问题...

首先,我想知道哪个命令允许我从char变量中测试一下



学习`&''运算符。您可能会发现

` |''和'^''运算符也很有趣,以及''>>''

和`< <''。

其次,我想编写一个代码检查程序。


不确定代码检查器是什么意思。是不是
与调度出租车有关?

我的代码是36位长,
但我不能做一个位数组或指针(我的编译器不接受它)。我怎么能建立一个?你能给我一些命令来帮助我??




C中最小的可寻址内存单位是

字节,即使底层硬件能够更精细地解决粒度问题。因此,C无法处理指向单个位的指针。而且由于数组元素

引用是根据指针定义的,所以它也会变成

,因为C不能生成位数。

因此,您需要考虑其他方式来表示

并访问每个代码的36位。

有很多方法;这里有几个:


- 使用整个'char''(任何风味)来存储每个位,

并且浪费其他位。您可以在这些膨胀位中形成数组

。形成指向它们的指针,并且

等等。缺点是,如果你有一个非常大的b / b $ b b代码浮动,那就是它吃了大量的内存。


- 将三十六位打包成一个至少具有该宽度的无符号整数

类型,并使用上面提到的按位

运算符以及相应的

单位掩码,单独操作它们。

这(通常)缩小了存储要求,

使代码更加繁琐的成本 - 但

大部分的笨拙可以隐藏在宏后面;

请参阅常见问题解答。


- 如果你不是有足够宽的积分类型

可用,你可以使用一小部分较窄的

对象。再次,请参阅常见问题解答。


-
Er ** *******@sun.com


" Sam(rép.sans-no-sp-am)"写道:


大家好!

我是C的新手,抱歉这两个问题...

首先,我想知道哪个命令允许我从char变量中测试一下



学习`&''运算符。您可能会发现

` |''和'^''运算符也很有趣,以及''>>''

和`< <''。

其次,我想编写一个代码检查程序。


不确定代码检查器是什么意思。是不是
与调度出租车有关?

我的代码是36位长,
但我不能做一个位数组或指针(我的编译器不接受它)。我怎么能建立一个?你能给我一些命令来帮助我??




C中最小的可寻址内存单位是

字节,即使底层硬件能够更精细地解决粒度问题。因此,C无法处理指向单个位的指针。而且由于数组元素

引用是根据指针定义的,所以它也会变成

,因为C不能生成位数。

因此,您需要考虑其他方式来表示

并访问每个代码的36位。

有很多方法;这里有几个:


- 使用整个'char''(任何风味)来存储每个位,

并且浪费其他位。您可以在这些膨胀位中形成数组

。形成指向它们的指针,并且

等等。缺点是,如果你有一个非常大的b / b $ b b代码浮动,那就是它吃了大量的内存。


- 将三十六位打包成一个至少具有该宽度的无符号整数

类型,并使用上面提到的按位

运算符以及相应的

单位掩码,单独操作它们。

这(通常)缩小了存储要求,

使代码更加繁琐的成本 - 但

大部分的笨拙可以隐藏在宏后面;

请参阅常见问题解答。


- 如果你不是有足够宽的积分类型

可用,你可以使用一小部分较窄的

对象。再次,请参阅常见问题解答。


-
Er ** *******@sun.com


Sam写道:

大家好!

我是C的新手,抱歉这两个问题...

首先,我想知道哪个命令


C有操作符和函数,但没有命令。

允许我从char变量中测试一下



从1.左移它到正确的位置。然后使用char变量按位-AND

结果。如果结果不为零,则设置要测试的位为
的位。否则,很明显。

其次,我想编写一个代码检查程序。我的代码是36位长,
但我不能做一个位数组或指针(我的编译器不接受它)。我怎么能建立一个?你能给我一些命令来帮助我??




你可以使用一个unsigned char数组来存储一个位图(一组位的

任意长度)。要保证36位存储空间,请至少使用5-char

数组。使用shift和按位-OR设置位,使用shift的组合,

按位-AND和按位-NOT清除它们,并且上面提到的技术是上面的b
来测试
详见常见问题解答。


-

Richard Heathfield: bi **** @ eton.powernet.co.uk

Usenet是一个奇怪的地方" - Dennis M Ritchie,1999年7月29日。

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton


Hi all !

I am new to C, sorry for those two questions ...

First, I would like to know which command allows me to test just one bit
from a char variable.

Second, I would like to programm a code checker. My code is 36 bits long,
but I can''t do a bit-array or pointer (my compiler doesn''t accept it). How
could I build one ? Could you give me a few commands to help me ??

Thanks a LOT !

Sam

解决方案

"Sam (rép. sans -no-sp-am)" wrote:


Hi all !

I am new to C, sorry for those two questions ...

First, I would like to know which command allows me to test just one bit
from a char variable.
Study the `&'' operator. You will probably find the
`|'' and `^'' operators interesting, too, as well as ''>>''
and `<<''.
Second, I would like to programm a code checker.
Not sure what you mean by a "code checker." Does it
have something to do with dispatching taxis?
My code is 36 bits long,
but I can''t do a bit-array or pointer (my compiler doesn''t accept it). How
could I build one ? Could you give me a few commands to help me ??



The smallest addressable unit of memory in C is the
byte, even if the underlying hardware is capable of finer
addressing granularity. As a consequence, C can''t handle
a pointer to an individual bit. And since array element
references are defined in terms of pointers, it also turns
out that C can''t make arrays of bits.

You therefore need to consider other ways to represent
and access the thirty-six bits of each of your codes. There
are lots of approaches; here are a few:

- Use an entire `char'' (any flavor) to store each bit,
and just waste the other bits. You can form arrays
of these "bloated bits," form pointers to them, and
so on. The disadvantage, if you have a very large
number of codes floating around, is that it eats
a lot of memory.

- Pack the thirty-six bits into an unsigned integral
type with at least that width, and use the bitwise
operators mentioned above, along with appropriate
single-bit masks, to manipulate them individually.
This (usually) shrinks the storage requirement, at
the cost of making the code more cumbersome -- but
much of the clumsiness can be hidden behind macros;
see the FAQ.

- If you don''t have a sufficiently wide integral type
available, you can use a small array of narrower
objects. Again, see the FAQ.

--
Er*********@sun.com


"Sam (rép. sans -no-sp-am)" wrote:


Hi all !

I am new to C, sorry for those two questions ...

First, I would like to know which command allows me to test just one bit
from a char variable.
Study the `&'' operator. You will probably find the
`|'' and `^'' operators interesting, too, as well as ''>>''
and `<<''.
Second, I would like to programm a code checker.
Not sure what you mean by a "code checker." Does it
have something to do with dispatching taxis?
My code is 36 bits long,
but I can''t do a bit-array or pointer (my compiler doesn''t accept it). How
could I build one ? Could you give me a few commands to help me ??



The smallest addressable unit of memory in C is the
byte, even if the underlying hardware is capable of finer
addressing granularity. As a consequence, C can''t handle
a pointer to an individual bit. And since array element
references are defined in terms of pointers, it also turns
out that C can''t make arrays of bits.

You therefore need to consider other ways to represent
and access the thirty-six bits of each of your codes. There
are lots of approaches; here are a few:

- Use an entire `char'' (any flavor) to store each bit,
and just waste the other bits. You can form arrays
of these "bloated bits," form pointers to them, and
so on. The disadvantage, if you have a very large
number of codes floating around, is that it eats
a lot of memory.

- Pack the thirty-six bits into an unsigned integral
type with at least that width, and use the bitwise
operators mentioned above, along with appropriate
single-bit masks, to manipulate them individually.
This (usually) shrinks the storage requirement, at
the cost of making the code more cumbersome -- but
much of the clumsiness can be hidden behind macros;
see the FAQ.

- If you don''t have a sufficiently wide integral type
available, you can use a small array of narrower
objects. Again, see the FAQ.

--
Er*********@sun.com


Sam wrote:

Hi all !

I am new to C, sorry for those two questions ...

First, I would like to know which command
C has operators and functions, but not commands.
allows me to test just one bit
from a char variable.
Start with 1. Left-shift it into the correct position. Then bitwise-AND the
result with your char variable. If the result is non-zero, the bit you wish
to test is set. Otherwise, it is clear.

Second, I would like to programm a code checker. My code is 36 bits long,
but I can''t do a bit-array or pointer (my compiler doesn''t accept it). How
could I build one ? Could you give me a few commands to help me ??



You can use an array of unsigned char to store a bitmap (a group of bits of
arbitrary length). To guarantee 36 bits of storage, use at least a 5-char
array. Use shift and bitwise-OR to set bits, use a combination of shift,
bitwise-AND and bitwise-NOT to clear them, and the technique mentioned
above to test them.

See the FAQ for more details.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


这篇关于测试一下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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