按位与 [英] bitwise-AND
问题描述
问候,
如果我有一个数值,可以说,uint value我想设置第5个
位,我不能只说value =(value& 0x20),因为它是一个布尔值
评估。我不能只添加((2 ^ 5)-1),因为我不能保证会将第5位设为
。
谢谢,
Shawn
Greetings,
If I havea a numeric value, lets say, "uint value" and I want to set the 5th
bit, I can''t just say "value = (value & 0x20) because it yeilds a boolean
evalutation. I can''t just add ((2^5)-1) because I am not guaranteed it will
set the 5th bit.
Thanks,
Shawn
推荐答案
" Shawn B." <乐**** @ html.com>在消息中写道
news:uv ************** @ TK2MSFTNGP09.phx.gbl ...
"Shawn B." <le****@html.com> wrote in message
news:uv**************@TK2MSFTNGP09.phx.gbl...
如果我有一个数字值,让我们说,uint值我想设置
第5位,我不能只说value =(value& 0x20),因为它是一个布尔值的评估。我不能只添加((2 ^ 5)-1)因为我不能保证
会设置第5位。
If I havea a numeric value, lets say, "uint value" and I want to set the 5th bit, I can''t just say "value = (value & 0x20) because it yeilds a boolean
evalutation. I can''t just add ((2^5)-1) because I am not guaranteed it will set the 5th bit.
要设置你要说的话
value = value | 0x20;
或
value | = 0x20;
- Alan
To set the bit you would say
value = value | 0x20;
or
value |= 0x20;
-- Alan
>
Shawn B.< le **** @ html.com>写道:
Shawn B. <le****@html.com> wrote:
如果我有一个数值,让我们说,uint value我想设置第5位,我不能只说value =(value& 0x20),因为它是一个布尔值的评估。
If I havea a numeric value, lets say, "uint value" and I want to set the 5th
bit, I can''t just say "value = (value & 0x20) because it yeilds a boolean
evalutation.
不,不是。 &安培;&安培;产生一个布尔结果,但&那并不。这是一个
的例子:
使用系统;
类测试
{
static void Main()
{
uint x = 21;
uint y = 5 ;
uint z = x& y;
Console.WriteLine(z);
}
}
-
Jon Skeet - < sk *** @ pobox.com>
如果回复该组,请不要给我发邮件
No it doesn''t. && yields a boolean result, but & doesn''t. Here''s an
example:
using System;
class Test
{
static void Main()
{
uint x = 21;
uint y = 5;
uint z = x&y;
Console.WriteLine (z);
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
清除钻头是否反过来? (也许我应该先尝试
问=))
谢谢,
Shawn
" ; Alan Pretre <无@垃圾>在消息中写道
news:eo ************** @ TK2MSFTNGP11.phx.gbl ...
Does the reverse work for clearing the bit? (perhaps I should try before
asking =))
Thanks,
Shawn
"Alan Pretre" <no@spam> wrote in message
news:eo**************@TK2MSFTNGP11.phx.gbl...
" Shawn B. " <乐**** @ html.com>在消息中写道
新闻:uv ************** @ TK2MSFTNGP09.phx.gbl ...
"Shawn B." <le****@html.com> wrote in message
news:uv**************@TK2MSFTNGP09.phx.gbl...
如果我有一个数值,让我们说,uint值我想设置
If I havea a numeric value, lets say, "uint value" and I want to set the
第5个
位,我不能只说value =(value& 0x20)因为它产生
布尔评估。我不能只添加((2 ^ 5)-1)因为我不能保证它
bit, I can''t just say "value = (value & 0x20) because it yeilds a boolean evalutation. I can''t just add ((2^5)-1) because I am not guaranteed it
将
设置第5位。
value = value | 0x20;
或
值| = 0x20;
- 艾伦
To set the bit you would say
value = value | 0x20;
or
value |= 0x20;
-- Alan
这篇关于按位与的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!