BITWISE SHIFT问题 [英] BITWISE SHIFT question

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

问题描述

好的,这是问题。我想在int

中提取前4位,所以让我们说


int b = somenumber;

int result = 0;

result = b<< 4;


如果我得到这个正确的结果应该包含4位变速器

到左边? :)如果我错了怎么能在变量中得到一个x的

位呢?

解决方案

2006年4月12日星期三22:52:04 -0700,HARDCORECODER写道:

这里好的是问题。我想在int中提取前4位,所以

int b = somenumber;
int result = 0;
result = b<< ; 4;

如果我得到了这个正确的结果,那么应该包含向右移动的4位数? :)如果我错了怎样才能将x位变为变量?




前四位是不明确的...并且取决于你的系统是否是b $ b大或小端...并且众所周知,小端机器很糟糕!


但是,为了回答你的问题,向左移动会增加一个功率值两个并且向右移动减少两个幂....所以,提取位

数字为2 ^ nn = [0..15]的数字中的字段使用


bits1to3 =(x>> 1)& 0x0f; //位1 2 3 4

bits12to15 =(x>> 12)& 0x0f; //位12 13 14 15


提取你应该向右移动的位字段然后使用位掩码

包含所有的字符。





noone写道:

2006年4月12日星期三22:52:04 -0700, HARDCORECODER写道:

这里好的是问题。我想在int中提取前4位,所以

int b = somenumber;
int result = 0;
result = b<< ; 4;

如果我得到了这个正确的结果,那么应该包含向右移动的4位数? :)如果我错了怎么能把x位变成变量?



前四位是不明确的...并且取决于你的系统是大端还是小端...并且众所周知,小端机器很糟糕!




In无论什么硬件位

顺序是,C和C ++位从右到左计数。因此可以通过向右移动N次来提取N',并使用该值作为位掩码来获得



unsigned x;

无符号n;

无符号bit_n = 0!= x& (1<< n);


别忘了在C和C ++中我们从0算起,所以最低的

顺序位索引为0.


HARDCORECODER写道:

这里好的是问题。我想提取int中的前4位

int b = somenumber;
int result = 0;
result = b<< ; 4;

如果我得到这个正确的结果应该包含右移的4位移位器? :)如果我错了怎样才能将x数量的
位变成变量?




Nope。 b<< 4仅表示b * 16,并且b>> 4表示b / 16.


什么是前四位无论如何?价值最低的那些?

还是最高的?如果有填充位怎么办?


HTH,

Michiel Salters


ok here is the question. I want to exract the first 4 bits in a int
so let say

int b = somenumber;
int result= 0;
result = b << 4;

if I got this right result should contain the 4 bits that were shifter
to the left right? :) and if I''m wrong how can i get an x number of
bits into a variable?

解决方案

On Wed, 12 Apr 2006 22:52:04 -0700, HARDCORECODER wrote:

ok here is the question. I want to exract the first 4 bits in a int so
let say

int b = somenumber;
int result= 0;
result = b << 4;

if I got this right result should contain the 4 bits that were shifter to
the left right? :) and if I''m wrong how can i get an x number of bits into
a variable?



"first four bits" is ambiguous...and depends upon whether your system is
big or little endian...and as everyone knows, little endian machines suck!

but, to answer your question, shift left increases the value by a power
of two and shift right decreases by a power of two....so, to extract bit
fields out of a number with bits numbers 2^n n=[0..15] use

bits1to3=(x>>1)&0x0f; // bits 1 2 3 4

bits12to15=(x>>12)&0x0f; // bits 12 13 14 15

to extract bit fields you should shift right then use a bit mask
containing all ones.




noone wrote:

On Wed, 12 Apr 2006 22:52:04 -0700, HARDCORECODER wrote:

ok here is the question. I want to exract the first 4 bits in a int so
let say

int b = somenumber;
int result= 0;
result = b << 4;

if I got this right result should contain the 4 bits that were shifter to
the left right? :) and if I''m wrong how can i get an x number of bits into
a variable?



"first four bits" is ambiguous...and depends upon whether your system is
big or little endian...and as everyone knows, little endian machines suck!



In C and C++ bits are counted right to left no matter what hardware bit
order is. N''s bit thus can be extracted by shifting 1 right N times and
using the value as a bit-mask:

unsigned x;
unsigned n;
unsigned bit_n = 0 != x & (1 << n);

Don''t forget that in C and C++ we count from 0, so that the lowest
order bit has index 0.


HARDCORECODER wrote:

ok here is the question. I want to exract the first 4 bits in a int
so let say

int b = somenumber;
int result= 0;
result = b << 4;

if I got this right result should contain the 4 bits that were shifter
to the left right? :) and if I''m wrong how can i get an x number of
bits into a variable?



Nope. b << 4 just means b * 16, and b >> 4 means b / 16.

What are "the first four bits" anyway? Those with the lowest value?
Or the highest? And what if there are padding bits?

HTH,
Michiel Salters


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

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