用于在C中切换第n位的程序 [英] Program to toggle nth bit in C

查看:53
本文介绍了用于在C中切换第n位的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




任何人都可以告诉我如何切换第n位。例如,我有一个8位变量的
。我想切换第n位。


问候,

Santosh

Hi,

Can anyone please tell me how to toggle a nth bit. For example, I am
having a variable which is of 8 bits. I want to toggle the nth bit.

Regards,
Santosh

推荐答案

sk*****@gmail.com 写道:
<有谁能告诉我如何切换第n位。

Can anyone please tell me how to toggle a nth bit.




尝试常见问题...

http://c-faq.com/misc/bitmanip.html


-

彼得



Try the FAQ...

http://c-faq.com/misc/bitmanip.html

--
Peter


哦,男孩......我很想建议你是一个变革者,或者明白告诉你,你有多大的身份。真!想知道为什么?一句话:

研究!你有没有试过用谷歌搜索关于按位的
C操作?我花了1分钟来证明我是对的,我发现

几个网站解释得很好。例如:
http://www.cprogramming.com /tutorial...operators.html


请尝试开始考虑自己的问题而不是在新闻组上询问愚蠢的

问题。我可以给你一个更好的答案,但

为什么?没有其他人,但你会读它,因为有那么多

资源,所以为什么要打扰。如果你给我一个合理的答案,我会给你发一个确切的代码来做SET,CLEAR或TOGGLE第N位。


希望我的回答可以帮助你无论如何,

Nikolay

sk*****@gmail.com D?D°D?D ??? D°:
Oh boy... I''m tempted to suggest you a carreer change or plainly tell
you how big of an id**t you are. Really! Want to know why? One word:
research! Did you ever tried searching with google about bitwise
operations in C? I spent 1 minute just to prove I was right and I found
a couple of sites explaining it very well. This one for example:
http://www.cprogramming.com/tutorial...operators.html

Please try to start thinking of your own instead of asking stupid
questions on the newsgroups. I could give you a much better answer, but
WHY? Noone else but you will ever read it because there are so many
resources out there so why bother. If you give me a reasonable answer I
will post you the exact code to do SET, CLEAR or TOGGLE the Nth bit.

Hope my answer helps you in any way,
Nikolay

sk*****@gmail.com D?D°D?D???D°:


任何人都可以告诉我如何切换第n位。例如,我有一个8位的变量。我想切换第n位。

问候,
Santosh
Hi,

Can anyone please tell me how to toggle a nth bit. For example, I am
having a variable which is of 8 bits. I want to toggle the nth bit.

Regards,
Santosh






ns ******* @ gmail.com 说:
哦,小男孩。 ..我很想建议你做一个改变或明白告诉你,你有多大的身份。


对我来说这似乎是一个合理的问题,虽然在常见问题解答和K& R中已经回答了这个问题,但这本来是明智之举。 OP首先检查。

真的!想知道为什么?一句话:研究!


好​​的,让我们看看你的研究在哪里带我们去...

您是否尝试过使用Google搜索C中的按位操作?


谷歌没有选择性。你可以写一个这样的页面:


+ ----------------------------- ----------------------------- +

| http://www.lousytips.disinformation.org/bitunwise.html |

+ --------------------------------------- ------------------- +

| |

| C中的按位运算|

| ----------------------- |

| |

| C不支持按位运算。请改用BASIC。 |

| |

+ ---------------------------------------- ------------------ +


....谷歌会毫无偏见地高兴地将其编入索引。


我花了1分钟来证明我是对的,我发现
有几个网站很好地解释了它。例如:
http://www.cprogramming.com /tutorial...operators.html
Oh boy... I''m tempted to suggest you a carreer change or plainly tell
you how big of an id**t you are.
It seemed like a reasonable question to me, albeit one that is answered in
the FAQ and in K&R, which it would have been wise of the OP to check first.
Really! Want to know why? One word: research!
Okay, let''s see where your "research" takes us...
Did you ever tried searching with google about bitwise operations in C?
Google is not selective. You could write a page like this:

+----------------------------------------------------------+
| http://www.lousytips.disinformation.org/bitunwise.html |
+----------------------------------------------------------+
| |
| Bitwise operations in C |
| ----------------------- |
| |
| C doesn''t support bitwise operations. Use BASIC instead. |
| |
+----------------------------------------------------------+

....and Google would cheerfully index it without prejudice.

I spent 1 minute just to prove I was right and I found
a couple of sites explaining it very well. This one for example:
http://www.cprogramming.com/tutorial...operators.html




该网站在C编程方面声誉很差。但是从来没有想过 - 让我们看看页面本身:


一般来说,作为程序员你不需要关心你自己在位级别上进行
操作。嗯,那也许是,但如果他不需要

来知道答案,他就不会问这个问题。就个人而言,我发现自己经常做位操作。他们不仅在密码学和图形学中有有用的应用程序,而且对于n位数字也是必不可少的(当n> sizeof(最大整数)时更是如此

类型)* CHAR_BIT),对Bloom过滤器这样的东西很有用。


经过三行半的广义文本后,我们发现(或者至少,我发现了对于Prismtech,Microsoft和National的广告,无论是什么

。一个天真的读者可能会停在那里,认为文章

结束了。在文本中散布广告是没有思想和剥削的。


按照惯例,在C和C ++中你可以考虑二进制数......


没有二进制数这样的东西。作者将数字

与数字表示混淆。


" 10000000是128"


不,它不是。


左右移位运算符的结果不是实现

依赖于无符号数字


是的,它们是,作者是否意味着无符号数字。或未签名

类型。 1L<< 1的结果32在某些系统上是未定义的,并且

在其他系统上定义良好。


"注意,在这个例子中,我们使用的是整数,是2或4

字节......"


目前尚不清楚作者是否意味着整数是保证是$

2或4个字节(它们不是)。但是,如果这不是什么意思,那么很难看出为什么作者会烦恼地说什么,因为

这个例子很简单:返回数字<<功率;这不是

依赖于任何特定的整数大小,除了允许的

电力值,这个例子根本没有检查。


" 128 * 2 = 256,我们甚至不能存储一个字节大的数字......


我们可以,如果CHAR_BIT> = 9.


该页面有其他此类错误,我不推荐它。


研究这么多。除了在Google上输入一些搜索

条款之外,还有更多研究。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)



That site has a very poor reputation with regard to C programming. But never
mind that - let''s look at the page itself:

"Generally, as a programmer you don''t need to concern yourself about
operations at the bit level." Well, that''s as maybe, but if he didn''t need
to know the answer he wouldn''t have asked the question. Personally, I find
myself doing bit manipulations fairly regularly. Not only do they have
useful applications in cryptography and graphics, but they are also
essential for n-bit numbers (the more so when n > sizeof(largest integer
type) * CHAR_BIT), and useful for such things as Bloom filters.

After three and a half lines of generalised text, we find (or at least, I
found) advertisements for Prismtech, Microsoft, and "National", whatever
that is. A naive reader might well stop right there, thinking the article
was over. Interspersing ads in the text is thoughtless and exploitative.

"By convention, in C and C++ you can think about binary numbers..."

There is no such thing as a binary number. The author is confusing number
with number representation.

"10000000 is 128"

No, it isn''t.

"the results of the left and right shift operators are not implementation
dependent for unsigned numbers"

Yes, they are, whether the author means "unsigned numbers" or "unsigned
types". The result of 1L << 32 is undefined on some systems, and
well-defined on others.

"Note that in this example, we''re using integers, which are either 2 or 4
bytes..."

It is not clear whether the author means that integers are guaranteed to be
2 or 4 bytes (which they are not). If that is not what is meant, though, it
is hard to see why the author would have bothered to say anything, since
the example in question is simply: return number << power; which is not
dependent on any particular integer size, except in terms of the allowable
values of power, which the example does not check at all.

"128 * 2 = 256, and we can''t even store a number that big in a byte..."

We can if CHAR_BIT >= 9.

The page has other such errors, and I cannot recommend it.

So much for "research". There is more to research than typing a few search
terms into Google.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


这篇关于用于在C中切换第n位的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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