按位操作... [英] bitwise operation...

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

问题描述

您好,


尝试从一个字节获取位偏移值。例如:


0x1 = 0

0x2 = 1

0x4 = 2

0x8 = 3

0x10 = 4

...

...

0x80 = 7




我需要这个值,所以我可以使用移位运算符''<<''或''>>''。我可以认为

有多种方法可以做到这一点但是它们在代码中看起来都很长。


我只能认为有一些快速按位获取位偏移的操作

值。


(请提供C代码)


TIA


Patrick。

Hello,

Trying to get the bit offset value from a byte. For example:

0x1 = 0
0x2 = 1
0x4 = 2
0x8 = 3
0x10 = 4
...
...
0x80 = 7
etc.

I need this value so I can use the shift operator ''<<'' or ''>>''. I can think
of a number of ways to do this but they all seem to be long in code.

I can only think there is some quick bitwise operation to get the bit offset
value.

(please provide code in ''C'')

TIA

Patrick.

推荐答案

Patrick Hoonhout< pl **** @ hotmail.com> ;潦草地写道:
Patrick Hoonhout <pl****@hotmail.com> scribbled the following:
Hello,
试图从一个字节中获取位偏移值。例如:
0x1 = 0
0x2 = 1
0x4 = 2
0x8 = 3
0x10 = 4
..
..
0x80 = 7
等等
我需要这个值,所以我可以使用移位运算符''<<''或''>>''。我可以想到有很多方法可以做到这一点但是它们在代码中看起来都很长。
我只能认为有一些快速按位操作来获得位偏移值。
(请提供''C'代码)
Hello, Trying to get the bit offset value from a byte. For example: 0x1 = 0
0x2 = 1
0x4 = 2
0x8 = 3
0x10 = 4
..
..
0x80 = 7
etc. I need this value so I can use the shift operator ''<<'' or ''>>''. I can think
of a number of ways to do this but they all seem to be long in code. I can only think there is some quick bitwise operation to get the bit offset
value. (please provide code in ''C'')




这是你的作业吗?这是一种快速而又肮脏的方式:


int byte = getbyte(); int bit = 0; while(byte>> = 1)bit ++;


此代码尚未经过测试,只能保证您发布的那些

字节值,并且它会修改原始字节值。

将其作为练习使其为其他字节值工作,

提供错误检查,正确的输入和输出,当然还有评论

并记录它。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)-------- ------------------- \

| 飞翔的柠檬树中的金鸡王G ++ FR FW + M-#108 D + ADA N +++ |

| http://www.helsinki.fi/~palaste W ++ B OP + |

\ -----------------------------------------芬兰的规则! ------------ /

正常就是其他人都是,而你却不是。

- 博士Tolian Soran



Is this for your homework? Here''s one quick and dirty way:

int byte=getbyte();int bit=0;while(byte>>=1)bit++;

This code hasn''t been tested, it is only guaranteed to work for those
byte values you posted, and it modifies the original byte value.
It is left as an exercise to make this work for other byte values,
provide error checking, proper input and ouput, and of course comment
and document it.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Normal is what everyone else is, and you''re not."
- Dr. Tolian Soran




" Alan Balmer" <人****** @ att.net>在消息中写道> >

"Alan Balmer" <al******@att.net> wrote in message > >
将字节映射到大多数任何特征的快速方法是使用
查找表(256个条目,假设CHAR_BIT为8.)你是否总是
去只有一套?

Al Balmer
Balmer Consulting
重新************************ @ att.net
The quick way to map a byte into most any characteristic is to use a
lookup table (256 entries, assuming CHAR_BIT is 8.) Are you always
going to have only one bit set?

--
Al Balmer
Balmer Consulting
re************************@att.net




是的,只需要一位。


Patrick。



Yes, just one bit set.

Patrick.


Patrick Hoonhout写道:
Patrick Hoonhout wrote:
" Alan Balmer" <人****** @ att.net>在消息中写道> >
"Alan Balmer" <al******@att.net> wrote in message > >
将字节映射到大多数特征的快速方法是使用
查找表(256个条目,假设CHAR_BIT为8.)你总是
The quick way to map a byte into most any characteristic is to use a
lookup table (256 entries, assuming CHAR_BIT is 8.) Are you always
going to have only one bit set?



是的,只需设置一位。



Yes, just one bit set.




unsigned char byte = 0x80 ,关闭;


开关(字节){

案例1:关闭= 0;

案例2:关闭= 1;

案例4:关闭= 2;

案例8:关闭= 3;

案例16:关闭= 4;

案例32:off = 5;

案例64:off = 6;

案例128:off = 7;

}


/ *或* /


int i;

unsigned char byte = 0x80,off,offs [256 ] = {0};

for(i = 0; i< 8; i ++)offs [1<< i] = i;


off = offs [byte];


Jirka



unsigned char byte = 0x80, off;

switch (byte) {
case 1: off = 0;
case 2: off = 1;
case 4: off = 2;
case 8: off = 3;
case 16: off = 4;
case 32: off = 5;
case 64: off = 6;
case 128: off = 7;
}

/* or */

int i;
unsigned char byte = 0x80, off, offs[256] = {0};
for (i=0; i<8; i++) offs[1 << i] = i;

off = offs[byte];

Jirka


这篇关于按位操作...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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