位复制 [英] bit copying
问题描述
嗨
我需要将0-6位从一个字节复制到一个字的0-6位。然后我需要将
从另一个字节的0-6位复制到这个单词的第7-14位。
我会因为乱搞而变得灰暗,或,>>和<<运营商。我希望这个小组的集体观众可以指出一个明显的解决方案
来解决这个问题,这样我就可以因为没有看到明显的问题而踢我自己。
我遇到的主要问题是从第二个字节复制时保留单词
的LO字节。
谢谢。
Hi
I need to copy bits 0-6 from a byte into bits 0-6 of a word. then i need to
copy bits 0-6 from another byte into bits 7-14 of this word.
I am going gray from messing around with AND, OR, >> and << operators. I
hope the collective audience of this group can point out an obvious solution
to this problem so that i can kick myself for not seeing the obvious.
The main problem i am experiencing is preserving the LO byte of the word
when copying from the 2nd byte.
Thanks.
推荐答案
curium在新闻中写道:3f ******************* ****@news.dial.pipex.com:
curium wrote in news:3f***********************@news.dial.pipex.com :
嗨
我需要将0-6位从一个字节复制到0-6位一句话。然后我需要将0-6位从另一个字节复制到这个单词的第7-14位。
我正在使用AND,OR,>>来解决这个问题。和<<运营商。
我希望这个小组的集体观众可以指出这个问题的一个明显的解决方案,这样我就可以踢自己看不到明显的了。
>我遇到的主要问题是从第二个字节复制时保留
字的LO字节。
Hi
I need to copy bits 0-6 from a byte into bits 0-6 of a word. then i
need to copy bits 0-6 from another byte into bits 7-14 of this word.
I am going gray from messing around with AND, OR, >> and << operators.
I hope the collective audience of this group can point out an obvious
solution to this problem so that i can kick myself for not seeing the
obvious.
The main problem i am experiencing is preserving the LO byte of the
word when copying from the 2nd byte.
无符号合并(unsigned char lo,unsigned char hi)
{
unsigned result = static_cast< unsigned>(lo)& 0x3F;
result | =(static_cast< unsigned>(hi)& 0x3F)<< 6;
返回rsult;
}
HTH
Rob。
-
http:// www.victim-prime.dsl.pipex.com/
Rob Williscroft写道:
Rob Williscroft wrote:
curium写道:
curium wrote:
我需要将0-6位从一个字节复制到一个字的0-6位。然后我需要将0-6位从另一个字节复制到这个单词的第7-14位。
我正在使用AND,OR,>>来解决这个问题。和<<
运营商。我希望这个小组的集体观众可以指出这个问题的一个明显的解决方案,这样我就可以踢自己
因为看不到明显的问题。
我的主要问题是体验是从第二个字节复制时保留单词的LO字节。
I need to copy bits 0-6 from a byte into bits 0-6 of a word. then i
need to copy bits 0-6 from another byte into bits 7-14 of this word.
I am going gray from messing around with AND, OR, >> and <<
operators. I hope the collective audience of this group can point
out an obvious solution to this problem so that i can kick myself
for not seeing the obvious.
The main problem i am experiencing is preserving the LO byte of
the word when copying from the 2nd byte.
无符号合并(unsigned char lo,unsigned char hi)
{
unsigned result = static_cast< unsigned>(lo)& 0x3F;
结果| =(static_cast< unsigned>(hi)& 0x3F)<< 6;
返回rsult;
}
unsigned merge( unsigned char lo, unsigned char hi )
{
unsigned result = static_cast< unsigned >(lo) & 0x3F;
result |= ( static_cast< unsigned >(hi) & 0x3F ) << 6;
return rsult;
}
因为有些白痴交叉发布到clc和clc ++,你有
发布了一些不适用于clc的内容所以让我们更改
函数体是有效的C,并希望它仍然有效C ++
:-)
unsigned int result;
result =((hi& 0x3f)<< 6)| (lo& 0x3f);
返回结果;
这些值永远不会导致整数溢出。没有
这样的东西,如AND或OR运算符。
-
查克F(cb *** *****@yahoo.com)(cb********@worldnet.att.net)
可用于咨询/临时嵌入式和系统。
< http://cbfalconer.home.att.net>使用worldnet地址!
Because some idiot cross posted to c.l.c and c.l.c++, you have
posted something that is not applicable on c.l.c. So lets change
the function body to be valid C, and hope it is still valid C++
:-)
unsigned int result;
result = ((hi & 0x3f) << 6) | (lo & 0x3f);
return result;
These values can never cause an integer overflow. There is no
such thing as an AND or an OR operator.
--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
>我不明白你的意思是
> I don''t understand what you mean by
没有AND或OR运算符这样的东西。
"There is no such thing as an AND or an OR operator."
AND和OR并且未在C中定义为关键字。
您将使用以下其中一项:
&
&&
|
||
AND and OR and not defined as keywords in C.
You''d use one of:
&
&&
|
||
这篇关于位复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!