bitshifting [英] bitshifting

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

问题描述

大家好,

我正试图理解变换的概念,并且有一些东西似乎让我无法理解。非常感谢你的回答。


1.当换位时,你正在使用某个位大小

(字节或更多?)或者可以你自己定义这个就像在
a struct中的位字段一样。我的意思是,当我移动一个

数字时,我不必定义一个位字段,就像我可以做到的那样。我在这里工作了多少位。

这是一个int的32位和char的一个字节吗?

2.你可以移动或分割多远?在unsigned int的边界内,最大功率为2

? (我现在只做轮换

无符号整数)

3.是MSB总是左边字节而LSB总是右边字节

(字节顺序可能有所不同)?为什么这两个重要呢?

4.当我移动4次时,我执行的是十进制数字* 4还是我的确是
做一个decimal_number * decimal_number * decimal_number *

decimal_number?

5.这段代码有什么作用? :

if(sscanf(t_addr,"%d。%d。%d。%d",

& octet1,& octet2,& octet3, & octet4)< 1)

exit_error(-1);

return((octet1<< 24)|(octet2<< 16)| (octet3<< 8)| octet4);


我在这里专注于最后一行。这些转换会向我显示一个32位ip地址的

逻辑,但当一个输入192为

的第一个八位位组时,小数也不会被24转为4608?

对我来说没什么意义,我不太明白

的推理是什么。


我习惯使用ip地址,所以对我来说,我数从128开始,

从1开始。当你使用超过1个字节时,你是否只去了256

512 1024等?我尝试了一个小程序,它给了我这个列表后

它再次变为0的最后一个数字。这是可以完成的最大金额

乘法吗?


2048

4096

8192

16384

32768

65536

131072

262144

524288

1048576

2097152

4194304

8388608

16777216

33554432

67108864

134217728

268435456

536870912

1073741824

2147483648

0


非常感谢大家

解决方案

Alef Veld写道:


大家好,

我想要得到我围绕着比特移位的概念,那里有一些东西似乎让我无法理解。非常感谢您的回答。



如果您想了解无符号整数的位移,请在纸上写下

二进制数并执行手动移动。


1.当移位时,是否有一个你正在处理的位大小

(字节或更多?)或者你可以自己定义这个与

结构中的位字段。我的意思是,当我移动一个数字时,我不需要定义一个位域

就像4我可以做到这一点。我在这里工作多少位。这是一个int的32位和一个字节的



这取决于你要转移的类型。


2.你能改变或分开多远?

中2的幂的最大值是无符号整数的边界? (我现在只对未签名的班次进行转换

整数)



四个无符号整数,你可以换到这个数字类型中的位数。


3.是MSB始终是左侧字节而LSB始终是右侧字节

(可能不同于字节顺序)?为什么这两个重要呢?



对于大多数用途,这没关系,重要的是价值超过

表示。
< blockquote class =post_quotes>
4.当我移动4次时,我是在执行decimal_number * 4还是我在
执行decimal_number * decimal_number * decimal_number * decimal_number?



当您移动4次时,您将乘以或除以2 ^ 4,即16。


5.这段代码有什么作用? :

return((octet1<< 24)|(octet2<< 16)|(octet3<< 8)| octet4);


我在这里专注于最后一行。这些转换会向我显示一个32位ip地址的

逻辑,但当一个输入192为

的第一个八位位组时,小数也不会被24转为4608?



这样看,


192 = 0x000000C0


Shift剩下的24位


0xc0000000


-

Ian Collins。


4月28日23:32,Ian Collins< ian-n ... @ hotmail.comwrote:


Alef Veld写道:


大家好,

我正试图理解位移的概念,并且那里有

是一些似乎无法避免的事情。非常感谢你的回答。



如果您想了解无符号整数的位移,请在纸上写下

二进制数并手动进行移位。 />


1.当移位时,你正在使用某个位大小

(字节或更多?)或者你可以自己定义它与

结构中的位字段一样。我的意思是,当我移动一个数字时,我不需要定义一个位域

就像4我可以做到这一点。我在这里工作多少位。这是一个int的32位和一个字节的



这取决于您正在转移的类型。


2.您可以移动或划分多远?

中2的幂的最大值是无符号整数的边界? (我现在只做无人签约的转移

整数)



四个无符号整数,你可以换到这个数字类型中的位数。



根据C89:如果

右边的操作数是负数,或者大于或等于,则结果(移位)是未定义的

左表达式类型中的位数。


这意味着''number''是无符号整数,即32位

宽,结果为

number<< 32



数字> 32

未定义。


问候Thomas Mertes


Seed7主页: http://seed7.sourceforge.net

Seed7 - 可扩展的编程语言:用户定义的语句

和运算符,抽象数据类型,没有特殊的模板

语法,OO with接口和多个分派,静态类型,
解释或编译,可移植,在linux / unix / windows下运行。


Alef Veld< al **@xs4all.nlwrites:


1.当移位时,你正在使用某个位大小

(字节或更多吗?)或者你可以自己定义这个与

a struct中的位字段一样。我的意思是,当我移动一个

数字时,我不必定义一个位字段,就像我可以做到的那样。我在这里使用了多少位

。这是一个int的32位和char的一个字节吗?



有两种情况。如果被移位的表达式是

较小的类型。比普通的int,值被提升,它是被转移的

提升值。当被移位的表达是

更大时这个类型是单独的。


我把更小的东西放在一边。和更大的在引号中,因为标准使用了很长的规则,基于一个名为

转换等级的概念。结果是价值被保留,包括

当然,标志,但不一定是签名。很容易

认为:


unsigned char x = 0xff;

x<< 24

将转移一个unsigned int,但它不会。因为类型int

可以表示unsigned char的所有可能值,所以它是一个普通的

(signed)int,它会被移位。这很重要,因为

签名班次的结果可能是未定义的(在这种情况下很可能)。


如果x已被声明为unsigned int ,转换将是一个

无符号的转换,因为unsigned int具有与int相同的转换等级。

大于的转移类型是在没有任何转换的情况下完成的,因此1ul<< 7

是无符号长值的移位。


2.你可以移动或分割多远?在unsigned int的边界内,最大功率为2

? (我现在只做轮换

无符号整数)



使用无符号类型,你可以向上移动(但是不等于

类型中的值位数。不值得记住签名类型的规则

,因为签名类型中的位操作通常是一个坏主意。 (规则是负值的右移是

实现定义,如果得到的

值无法表示,则左移不定义。在上面的例子中,如果256 * pow(2,24)

是INT_MAX(可能在32位机器上),结果将是

undefined。)


3.是MSB总是左边的字节而LSB总是右边的字节

(字节顺序可能有所不同)?为什么这两个重要无论如何




班次是根据他们对价值的作用来定义的(乘以
并除以2的幂)。字节顺序完全没有效果。


4.当我移动4次时,我执行的是十进制数字* 4还是我的确是
做一个decimal_number * decimal_number * decimal_number *

decimal_number?



左移4位的效果是将
的值乘以16.我不这样做,因为你可能是建议,将它乘以
4,也不会将其提高到4次幂。


(术语十进制指的是一种表示方式这个数字。

没有任何关于价值的信息。十进制数字* 4会更好地表达

数字* 4 - 十进制部分只是令人困惑。)


5.这段代码有什么作用? :

if(sscanf(t_addr,"%d。%d。%d。%d",

& octet1,& octet2,& octet3, & octet4)< 1)

exit_error(-1);

return((octet1<< 24)|(octet2<< 16)| (octet3<< 8)| octet4);



看起来很麻烦。你无法肯定地知道这些变化是什么,因为我们不知道octet1-4变量的类型。它们是由%d的sscanf格式设置的,因此它们应该是int。在这种情况下,

班次签署可能会遇到各种各样的麻烦。如果它们不是
int对象,则sscanf调用是错误的。

IP地址计算应使用无符号长变量或

在需要的地方使用无符号长演员。


我在这里专注于最后一行。转移将显示给我

32位ip地址的逻辑,但当一个输入192为

时,第一个八位位组小数也不会被移位24得到4608

?这对我来说没什么意义,我不太明白

这是什么原因。



我不跟进。如果x << 24被正确定义它具有

乘以pow(2,24)(2到24的幂)的效果。通常情况下,这将是
将x的值放在4字节值的MSB中。


我习惯工作有了ip地址,所以对我来说,我数从128开始,

从1开始。当你使用超过1个字节时,你只需要去256

512 1024等?我尝试了一个小程序,它给了我这个列表后

它再次变为0的最后一个数字。这是可以完成的最大数量的

乘法吗?



如果您发布代码,我相信它可以解释,但我不能按照你要求的那样跟随b $ b。


-

Ben。


Hi everyone,
I''m trying to get my head around the concept of bit shifting, and there
are some things that seem to elude me. Many thanks for your answers.

1. When shifting bits, is there a certain bit size you are working on
(byte or more?) or can you define this yourself as with a bit field in
a struct. I mean i don''t have to define a bit field when i shift a
number like 4 i can just do it. How many bits am i working with here.
Is that 32 bits for an int and a byte for a char ?
2. How far can you shift or divide ? To the max of the power of 2
within the boundaries of a unsigned int ? (i''m only doing shifts on
unsigned ints for now)
3. is the MSB always the left byte and the LSB always the right byte
(probably differs on endianness) ? Why are these 2 important anyway ?
4. When i shift 4 times, am i performing a decimal_number * 4 or am i
doing a decimal_number * decimal_number * decimal_number *
decimal_number ?
5. What does this code do ? :
if(sscanf(t_addr, "%d.%d.%d.%d",
&octet1, &octet2, &octet3, &octet4) < 1)
exit_error(-1);
return((octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4);

I''m focussing on the last line here. The shifts would display to me the
logic of an 32bit ip address but would when one entered 192 for the
first octet the decimal also not get shifted by 24 getting 4608 ?which
makes not a lot of sense to me and i don''t quite understand what the
reasoning for that is.

I''m used to working with ip addresses so for me i count from 128 and
start at 1. When working with more then 1 byte, do you just go to 256
512 1024 etc ? I tried a little program and it gave me this list after
the last number it went to 0 again. Is that the maximum amount of
multiplications that can be done ?

2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
2147483648
0

Many thanks everyone

解决方案

Alef Veld wrote:

Hi everyone,
I''m trying to get my head around the concept of bit shifting, and there
are some things that seem to elude me. Many thanks for your answers.

If you want to understand bit shifting for unsigned integers, write the
binary number on paper and do the shifting by hand.

1. When shifting bits, is there a certain bit size you are working on
(byte or more?) or can you define this yourself as with a bit field in a
struct. I mean i don''t have to define a bit field when i shift a number
like 4 i can just do it. How many bits am i working with here. Is that
32 bits for an int and a byte for a char ?

That depends on the type you are shifting.

2. How far can you shift or divide ? To the max of the power of 2 within
the boundaries of a unsigned int ? (i''m only doing shifts on unsigned
ints for now)

Four an unsigned int, you can shift up to the number of bits in the type.

3. is the MSB always the left byte and the LSB always the right byte
(probably differs on endianness) ? Why are these 2 important anyway ?

For most uses, this doesn''t matter, what matters is the value more than
the representation.

4. When i shift 4 times, am i performing a decimal_number * 4 or am i
doing a decimal_number * decimal_number * decimal_number * decimal_number ?

When you shift 4 times, you are multiplying or dividing by 2^4, which is 16.

5. What does this code do ? :
return((octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4);

I''m focussing on the last line here. The shifts would display to me the
logic of an 32bit ip address but would when one entered 192 for the
first octet the decimal also not get shifted by 24 getting 4608 ?

Look at it this way,

192 = 0x000000C0

Shift left 24 bits

0xc0000000

--
Ian Collins.


On 28 Apr., 23:32, Ian Collins <ian-n...@hotmail.comwrote:

Alef Veld wrote:

Hi everyone,
I''m trying to get my head around the concept of bit shifting, and there
are some things that seem to elude me. Many thanks for your answers.


If you want to understand bit shifting for unsigned integers, write the
binary number on paper and do the shifting by hand.

1. When shifting bits, is there a certain bit size you are working on
(byte or more?) or can you define this yourself as with a bit field in a
struct. I mean i don''t have to define a bit field when i shift a number
like 4 i can just do it. How many bits am i working with here. Is that
32 bits for an int and a byte for a char ?


That depends on the type you are shifting.

2. How far can you shift or divide ? To the max of the power of 2 within
the boundaries of a unsigned int ? (i''m only doing shifts on unsigned
ints for now)


Four an unsigned int, you can shift up to the number of bits in the type.

According to C89: The result (of a shift) is undefined if
the right operand is negative, or greater than or equal to
the number of bits in the left expression''s type.

That means if ''number'' is an unsigned int which is 32 bits
wide, the result of
number << 32
and
number >32
is undefined.

Greetings Thomas Mertes

Seed7 Homepage: http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.


Alef Veld <al**@xs4all.nlwrites:

1. When shifting bits, is there a certain bit size you are working on
(byte or more?) or can you define this yourself as with a bit field in
a struct. I mean i don''t have to define a bit field when i shift a
number like 4 i can just do it. How many bits am i working with
here. Is that 32 bits for an int and a byte for a char ?

There are two cases. If the expression being shifted is of a type
"smaller" than plain int, the value is promoted and it is the
promoted value that is shifted. When the expression being shifted is
"bigger" than plain int the type is left alone.

I put "smaller" and "bigger" in quotes because the standard uses quite
a lengthy set of rules based a notion called the conversion rank of
the type. The upshot is that the value is preserved including, of
course, the sign but not necessarily the signedness. It is easy to
think that:

unsigned char x = 0xff;
x << 24

will be shifting an unsigned int but it won''t. Because the type int
can represent all the possible values of unsigned char it is a plain
(signed) int that gets shifted. This matters, because the result of a
signed shift can be undefined (and may well be in this case).

If x had been declared unsigned int, the shift would have been an
unsigned one because unsigned int has the same conversion rank as int.
Shifts of "bigger" types are done without any conversion so 1ul << 7
is a shift of an unsigned long value.

2. How far can you shift or divide ? To the max of the power of 2
within the boundaries of a unsigned int ? (i''m only doing shifts on
unsigned ints for now)

With an unsigned type you can shift by up to (but not equal to) the
number of value bits in the type. It not worth remembering the rules
for signed types since bit operations in signed types are generally a
bad idea. (The rules are that right shifts of negative values are
implementation defined, and left shifts are undefined if the resulting
value can''t be represented. In the example above, if 256 * pow(2, 24)
is INT_MAX (probable on a 32 bit machine) the result will be
undefined.)

3. is the MSB always the left byte and the LSB always the right byte
(probably differs on endianness) ? Why are these 2 important anyway
?

Shifts are defined in terms of what they do to the value (multiplying
and dividing by powers of 2). The endianness has no effect at all.

4. When i shift 4 times, am i performing a decimal_number * 4 or am i
doing a decimal_number * decimal_number * decimal_number *
decimal_number ?

A left shift by 4 bits has the effect of multiplying the value being
shifted by 16. I does not, as you might be suggesting, multiply it by
4, nor does it raise it to the 4th power.

(The term "decimal" refers to a way of representing a number. It says
nothing about the value. decimal_number * 4 would be better expressed
as number * 4 -- the "decimal" part is just confusing.)

5. What does this code do ? :
if(sscanf(t_addr, "%d.%d.%d.%d",
&octet1, &octet2, &octet3, &octet4) < 1)
exit_error(-1);
return((octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4);

Looks like trouble. You can''t tell with certainty what the shifts do
because we don''t know the types of the octet1-4 variables. They are
set by a sscanf format of %d so they should int. In that case the
shifts are signed you maybe in all sorts of trouble. If they are not
int objects, then the sscanf call is wrong.

IP address calculations should be done with unsigned long variables or
with unsigned long casts where needed.

I''m focussing on the last line here. The shifts would display to me
the logic of an 32bit ip address but would when one entered 192 for
the first octet the decimal also not get shifted by 24 getting 4608
?which makes not a lot of sense to me and i don''t quite understand
what the reasoning for that is.

I don''t follow. If x << 24 is properly defined it has the effect of
multiplying by pow(2, 24) (2 to the power 24). Typically, that will
put the value of x in the MSB of a 4-byte value.

I''m used to working with ip addresses so for me i count from 128 and
start at 1. When working with more then 1 byte, do you just go to 256
512 1024 etc ? I tried a little program and it gave me this list after
the last number it went to 0 again. Is that the maximum amount of
multiplications that can be done ?

If you post the code, I am sure it can be explained but I can''t follow
what you are asking.

--
Ben.


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

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