为什么按位运算符? [英] Why bitwise operators?

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

问题描述



为什么会使用按位运算符?我可以用各种语言编写一些形状或形式(C ++,PHP,一些脚本),我之前已经听过/看过按位

运算符,但从未理解过为什么有人会使用它们 - 任何真实的

世界的例子或想法?示例如下(我在我的核心阅读

JavaScript指南1.5)。


15& 9收益9(1111& 1001 = 1001)

15 | 9收益率15(1111 | 1001 = 1111)

15 ^ 9收益率6(1111 ^ 1001 = 0110)

此外还有AND,OR,XOR,NOT和左右移动,这将是b / b
只延长这个帖子的时间超过必要...


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么顶级发布这么糟糕的事情?

A:热门发布。

问:usenet上最烦人的事情是什么?


Why would one use bitwise operators? I can program in various languages in
some shape or form (C++, PHP, some scripting) and I''ve heard/seen bitwise
operators before, but never understood why anyone would use them - any real
world examples or ideas? Examples follow (that I am reading in my Core
JavaScript Guide 1.5).

15 & 9 yields 9 (1111 & 1001 = 1001)
15 | 9 yields 15 (1111 | 1001 = 1111)
15 ^ 9 yields 6 (1111 ^ 1001 = 0110)
in addition there are AND,OR,XOR,NOT and left and right shifts which would
only extend this post longer than nescessary...

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet?

推荐答案

" Randell D." <哟************************** @ yahoo.com>写道:
"Randell D." <yo**************************@yahoo.com> writes:
为什么会使用按位运算符?
Why would one use bitwise operators?




我会说很少需要它。


在C语言中,你经常不得不乱拨,因为你做了大部分直接在比特上进行I / O编程。你必须切换位

并实现你自己的位向量。它使得这种低级语言中的位操作成为完美的优势




在Javascript中,您可以将位向量实现为整数并且使用

按位运算符。你也可以把它变成一组布尔值和

创建你需要的方法。后者可以更好地扩展。


有些情况下,整数运算可以更容易,或者b / b $ b更快,可以通过按位运算实现。我知道有,

我现在不能记住它们中的任何一个。


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

Art D''HTML :< URL:http://www.infimum.dk/HTML/randomArtSplit.html>

''没有判断的信仰只会降低精神神圣。''



I would say that it should rarely be needed.

In C, you often had to fiddle with bits, since you did most
I/O-programming directly on the bits. You had to toggle bits
and implement your own bit vectors. It made perfect senst
to have bit-operations in such a low-level language.

In Javascript, you can implement a bit vector as an integer and use
bitwise operators. You could also make it an array of booleans and
create the methods you need. The latter scales better.

There are still cases, where integer arithmetic can more easily, or
just faster, be implemented with bitwise operations. I know there are,
I just can''t remember any of them right now.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D''HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
''Faith without judgement merely degrades the spirit divine.''


2003年10月1日星期三,格林尼治标准时间22:18:53,在comp.lang.javascriptRandell D.

< yo ****** ********************@yahoo.com>写道:
On Wed, 01 Oct 2003 22:18:53 GMT, in comp.lang.javascript "Randell D."
<yo**************************@yahoo.com> wrote:
|
|为什么会使用按位运算符?我可以用
|中的各种语言编程一些形状或形式(C ++,PHP,一些脚本),我听到/看到按位
|以前的操作员,但从来没有理解为什么有人会使用它们 - 任何真正的
|世界的例子或想法?示例如下(我正在阅读我的核心
| JavaScript指南1.5)。
|
| 15& 9收率9(1111& 1001 = 1001)
| 15 | 9收益率15(1111 | 1001 = 1111)
| 15 ^ 9收益率6(1111 ^ 1001 = 0110)
|此外还有AND,OR,XOR,NOT和左右移位,这将是
|只延长这篇文章的时间超过了必要...
|
| Why would one use bitwise operators? I can program in various languages in
| some shape or form (C++, PHP, some scripting) and I''ve heard/seen bitwise
| operators before, but never understood why anyone would use them - any real
| world examples or ideas? Examples follow (that I am reading in my Core
| JavaScript Guide 1.5).
|
| 15 & 9 yields 9 (1111 & 1001 = 1001)
| 15 | 9 yields 15 (1111 | 1001 = 1111)
| 15 ^ 9 yields 6 (1111 ^ 1001 = 0110)
| in addition there are AND,OR,XOR,NOT and left and right shifts which would
| only extend this post longer than nescessary...




我刚刚用过这样一个例子。我需要为访问不同网页的不同人设置一些表格



而不是维护12个以上的变量;我将值映射到一个

单变量。现在我需要做的就是放置一些代码:

if(security_bits& CONST_dept_name)

并从那里处理。


我也可以使用

if(security_bits&(CONST_dnm1 + CONST_dnm2 + CONST_dnm3))


这是服务器端脚本。使用这个

的客户端,我看不出多少好处。

------------------- --------------------------------------------
jn **** @ yourpantsbigpond.net.au :脱掉裤子回复

--- -------------------------------------------------- ----------



I''ve just recently used such an example. I needed to set up some form
of security for different people accessing different web pages.
Instead of maintaining 12+ variables; I bitmapped the values into a
single variable. Now all I need to do is place some code like:
if( security_bits & CONST_dept_name )
and process from there.

I could also use
if( security_bits & (CONST_dnm1 + CONST_dnm2 + CONST_dnm3) )

This is server-side scripting. I can''t see much benefit in using this
client-side.
---------------------------------------------------------------
jn****@yourpantsbigpond.net.au : Remove your pants to reply
---------------------------------------------------------------




" Lasse Reichstein Nielsen" < lr*@hotpop.com>在消息中写道

news:7k ********** @ hotpop.com ...

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:7k**********@hotpop.com...
" Randell D." <哟************************** @ yahoo.com>写道:
"Randell D." <yo**************************@yahoo.com> writes:
为什么要使用按位运算符?
Why would one use bitwise operators?



我想说它应该很少需要。
在C语言中,你经常不得不使用位,因为你直接在位上做了大部分的I / O编程。你必须切换位
并实现自己的位向量。它使得这种低级语言中的位操作变得非常完美。

在Javascript中,您可以将位向量实现为整数并使用按位运算符。你也可以把它变成一个布尔数组,并创建你需要的方法。后者可以更好地扩展。

有些情况下,整数运算可以更容易,或者更快,可以通过按位运算实现。我知道有,
我现在不能记住它们中的任何一个。

/ L
-
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D''HTML:< URL:http://www.infimum.dk/HTML/randomArtSplit.html> ;'没有判断的信仰只会降低神灵的精神。''



I would say that it should rarely be needed.

In C, you often had to fiddle with bits, since you did most
I/O-programming directly on the bits. You had to toggle bits
and implement your own bit vectors. It made perfect senst
to have bit-operations in such a low-level language.

In Javascript, you can implement a bit vector as an integer and use
bitwise operators. You could also make it an array of booleans and
create the methods you need. The latter scales better.

There are still cases, where integer arithmetic can more easily, or
just faster, be implemented with bitwise operations. I know there are,
I just can''t remember any of them right now.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D''HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
''Faith without judgement merely degrades the spirit divine.''




我很高兴它是一个很少使用的设施/功能......我我非常肯定我可以获得

以及没有它的javascript以便套装我很好。


干杯

randell d 。



I''m glad its a rarely used facility/function... I''m pretty sure I can get
along with javascript without it so that suites me fine.

Cheers
randell d.


这篇关于为什么按位运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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