📄 位运算简介及实用技巧(二):进阶篇(1).htm
字号:
<---原数<BR>+---+---+---+---+---+---+---+---+<BR>| 1
1 | 1 0 | 0
0 | 1 1 |
<---第一次运算后<BR>+-------+-------+-------+-------+<BR>| 1
0 1 1 | 1 1 0
0 |
<---第二次运算后<BR>+---------------+---------------+<BR>| 1
1 0 0 1 0 1 1 |
<---第三次运算后<BR>+-------------------------------+</SPAN><BR><BR><BR><STRONG>Copyright也很强</STRONG><BR>
<DIV class=UBBPanel>
<DIV class=UBBTitle><IMG style="MARGIN: 0px 2px -3px 0px" alt=程序代码
src="位运算简介及实用技巧(二):进阶篇(1).files/quote.gif"> 程序代码</DIV>
<DIV class=UBBContent><PRE><CODE class=pascal>writeln('Matrix' , 42 XOR 105 , '原创,转贴请注明出处');</CODE></PRE></DIV></DIV><BR><BR><BR></DIV>
<SCRIPT type=text/javascript><!--google_ad_client = "pub-8918918108662869";google_ad_width = 728;google_ad_height = 90;google_ad_format = "728x90_as";google_ad_type = "text_image";google_ad_channel = "";google_color_border = "CCCCCC";google_color_bg = "F3F3F3";google_color_link = "6060FF";google_color_text = "292929";google_color_url = "000000";//--></SCRIPT>
<SCRIPT src="位运算简介及实用技巧(二):进阶篇(1).files/show_ads.js"
type=text/javascript></SCRIPT>
<DIV class=Content-body><IMG style="MARGIN: 0px 2px -4px 0px" alt=""
src="位运算简介及实用技巧(二):进阶篇(1).files/icon_trackback.gif"><STRONG>引用通告地址:</STRONG>
http://www.matrix67.com/blog/trackback.asp?tbID=JOFNONQ0&key=JOKOJQDPMMDQM8<BR><IMG
style="MARGIN: 4px 2px -4px 0px" alt=""
src="位运算简介及实用技巧(二):进阶篇(1).files/tag.gif"><STRONG>Tags:</STRONG> <A
href="http://www.matrix67.com/blog/default.asp?tag=%E4%BD%8D%E8%BF%90%E7%AE%97">位运算</A><A
style="DISPLAY: none" href="http://technorati.com/tag/位运算" rel=tag>位运算</A> <A
href="http://www.matrix67.com/blog/default.asp?tag=%E4%BA%8C%E8%BF%9B%E5%88%B6">二进制</A><A
style="DISPLAY: none" href="http://technorati.com/tag/二进制" rel=tag>二进制</A> <A
href="http://www.matrix67.com/blog/default.asp?tag=C%E8%AF%AD%E8%A8%80">C语言</A><A
style="DISPLAY: none" href="http://technorati.com/tag/C语言" rel=tag>C语言</A> <A
href="http://www.matrix67.com/blog/default.asp?tag=Pascal%E8%AF%AD%E8%A8%80">Pascal语言</A><A
style="DISPLAY: none" href="http://technorati.com/tag/Pascal语言"
rel=tag>Pascal语言</A> <A
href="http://www.matrix67.com/blog/default.asp?tag=%E4%BB%A3%E7%A0%81">代码</A><A
style="DISPLAY: none" href="http://technorati.com/tag/代码" rel=tag>代码</A> <A
href="http://www.matrix67.com/blog/default.asp?tag=%E7%AE%97%E6%B3%95">算法</A><A
style="DISPLAY: none" href="http://technorati.com/tag/算法" rel=tag>算法</A> <A
href="http://www.matrix67.com/blog/default.asp?tag=%E5%88%86%E6%B2%BB">分治</A><A
style="DISPLAY: none" href="http://technorati.com/tag/分治" rel=tag>分治</A> <A
href="http://www.matrix67.com/blog/default.asp?tag=%E8%B6%A3%E9%A2%98">趣题</A><A
style="DISPLAY: none" href="http://technorati.com/tag/趣题" rel=tag>趣题</A> <A
href="http://www.matrix67.com/blog/default.asp?tag=%E8%AF%81%E6%98%8E">证明</A><A
style="DISPLAY: none" href="http://technorati.com/tag/证明" rel=tag>证明</A> <BR><!--Add By WBC --><IMG style="MARGIN: 4px 2px -4px 0px" alt=""
src="位运算简介及实用技巧(二):进阶篇(1).files/tag.gif"><STRONG>我猜你可能喜欢:</STRONG><BR>
<DIV class=Content-body id=wbc_tag></DIV><BR><!--End By WBC --></DIV>
<DIV class=Content-bottom>
<DIV class=ContentBLeft></DIV>
<DIV class=ContentBRight></DIV>评论: 17</A> | <A
href="http://www.matrix67.com/blog/trackback.asp?tbID=JOFNONQ0&key=JOKOJQDPMMDQM8"
target=_blank>引用: 0</A> | 查看次数: 3479 </DIV></DIV></DIV><A accessKey=C
href="http://www.matrix67.com/blog/article.asp?id=312#comm_top"
name=comm_top></A>
<DIV class=pageContent>
<DIV class=page style="FLOAT: right">
<UL>
<LI class=pageNumber><STRONG>1</STRONG> | <A
href="http://www.matrix67.com/blog/article.asp?id=312&page=2#comm_top">2</A>
| <A title=下一页 style="TEXT-DECORATION: none" accessKey=.
href="http://www.matrix67.com/blog/article.asp?id=312&page=2"></A><A
title=最后一页 style="TEXT-DECORATION: none"
href="http://www.matrix67.com/blog/article.asp?id=312&page=2#comm_top">></A></LI></UL></DIV></DIV>
<DIV class=comment>
<DIV class=commenttop><A href="javascript:addQuote('Etfl','commcontent_1790')"
name=comm_1790><IMG style="MARGIN: 0px 4px -3px 0px" alt=""
src="位运算简介及实用技巧(二):进阶篇(1).files/icon_quote.gif" border=0></A> <A
href="http://www.matrix67.com/blog/member.asp?action=view&memName=Etfl"
target=_blank><STRONG>Etfl</STRONG></A> <SPAN class=commentinfo>[楼层: 17楼 发表时间:
2007-10-30 10:06 PM]</SPAN></DIV>
<DIV class=commentcontent id=commcontent_1790>为什么取绝对值时用<BR>x:=x and not(1 shl
31);<BR>当x为负数时会出201错误?(x是longint)</DIV></DIV>
<DIV class=comment>
<DIV class=commenttop><A href="javascript:addQuote('bar','commcontent_1745')"
name=comm_1745><IMG style="MARGIN: 0px 4px -3px 0px" alt=""
src="位运算简介及实用技巧(二):进阶篇(1).files/icon_quote.gif" border=0></A> bar <SPAN
class=commentinfo>[楼层: 16楼 发表时间: 2007-10-25 03:03 PM]</SPAN></DIV>
<DIV class=commentcontent
id=commcontent_1745>只用位运算来取绝对值<BR> 这是一个非常有趣的问题。大家先自己想想吧,Ctrl+A显示答案。<BR> 答案:假设x为32位整数,则x
xor (not (x shr 31) + 1) + x shr 31的结果是x的绝对值<BR> x shr
31是二进制的最高位,它用来表示x的符号。如果它为0(x为正),则not (x shr 31) +
1等于$00000000,异或任何数结果都不变;如果最高位为1(x为负),则not (x shr 31) +
1等于$FFFFFFFF,x异或它相当于所有数位取反,异或完后再加一。<BR>___________________________________________________________________________<BR>这个实现跟平台相关吧,有符号数左移,右移是未定义的行为吧。比如你这里的
x shr 31,如果x为负数的话,那么<BR>x shr 31就为0xffffffff, not (x shr 31) + 1
等于0x00000000了。</DIV></DIV>
<DIV class=comment>
<DIV class=commenttop><A href="javascript:addQuote('axgle','commcontent_958')"
name=comm_958><IMG style="MARGIN: 0px 4px -3px 0px" alt=""
src="位运算简介及实用技巧(二):进阶篇(1).files/icon_quote.gif" border=0></A> <A
href="http://hi.baidu.com/axgle" target=_blank><STRONG>axgle</STRONG></A> <SPAN
class=commentinfo>[楼层: 15楼 发表时间: 2007-07-29 02:14 PM]</SPAN></DIV>
<DIV class=commentcontent id=commcontent_958>writeln('Matrix' , 42 XOR 105 ,
'原创,转贴请注明出处');<BR>42 XOR 105 = 67 ?<BR><IMG style="MARGIN: 0px 0px -2px" alt=""
src="位运算简介及实用技巧(二):进阶篇(1).files/Face_02.gif" border=0><BR><BR><SPAN
style="COLOR: red">回复:嗯</SPAN></DIV></DIV>
<DIV class=comment>
<DIV class=commenttop><A href="javascript:addQuote('gromy','commcontent_940')"
name=comm_940><IMG style="MARGIN: 0px 4px -3px 0px" alt=""
src="位运算简介及实用技巧(二):进阶篇(1).files/icon_quote.gif" border=0></A> <A
href="http://gromy@blogbus.com/" target=_blank><STRONG>gromy</STRONG></A> <SPAN
class=commentinfo>[楼层: 14楼 发表时间: 2007-07-27 09:19 PM]</SPAN></DIV>
<DIV class=commentcontent
id=commcontent_940>哈哈!你太有个性了!211就211吧!还非要突出是你们班某MM的生日!哈哈<BR><BR><SPAN
style="COLOR: red">回复:突出那个MM在我心中的地位,随时忘不了提到她啊……</SPAN></DIV></DIV>
<DIV class=comment>
<DIV class=commenttop><A href="javascript:addQuote('axgle','commcontent_905')"
name=comm_905><IMG style="MARGIN: 0px 4px -3px 0px" alt=""
src="位运算简介及实用技巧(二):进阶篇(1).files/icon_quote.gif" border=0></A> <A
href="http://hi.baidu.com/axgle" target=_blank><STRONG>axgle</STRONG></A> <SPAN
class=commentinfo>[楼层: 12a楼 发表时间: 2007-07-25 04:32 PM]</SPAN></DIV>
<DIV class=commentcontent id=commcontent_905>午餐时候想到的,更简捷的写法:<BR>puts
sprintf("%032b",1314520).reverse.to_i(2)</DIV></DIV>
<DIV class=comment>
<DIV class=commenttop><A href="javascript:addQuote('axgle','commcontent_903')"
name=comm_903><IMG style="MARGIN: 0px 4px -3px 0px" alt=""
src="位运算简介及实用技巧(二):进阶篇(1).files/icon_quote.gif" border=0></A> <A
href="http://hi.baidu.com/axgle" target=_blank><STRONG>axgle</STRONG></A> <SPAN
class=commentinfo>[楼层: 12楼 发表时间: 2007-07-25 11:23 AM]</SPAN></DIV>
<DIV class=commentcontent
id=commcontent_903>二进制逆序<BR> 下面的程序读入一个32位整数并输出它的二进制倒序后所表示的数。<BR> 输入:
1314520 (二进制为00000000000101000000111011011000)<BR> 输出:
460335104 (二进制为00011011011100000010100000000000)<BR>--------ruby语言,一行代码解决问题:<BR>puts
sprintf("%032b","1314520").split(//).reverse.join.to_i(2)</DIV></DIV>
<DIV class=comment>
<DIV class=commenttop><A href="javascript:addQuote('axgle','commcontent_902')"
name=comm_902><IMG style="MARGIN: 0px 4px -3px 0px" alt=""
src="位运算简介及实用技巧(二):进阶篇(1).files/icon_quote.gif" border=0></A> <A
href="http://hi.baidu.com/axgle" target=_blank><STRONG>axgle</STRONG></A> <SPAN
class=commentinfo>[楼层: 11楼 发表时间: 2007-07-25 10:54 AM]</SPAN></DIV>
<DIV class=commentcontent
id=commcontent_902>例如,1314520的二进制101000000111011011000中有9个1,则x=1314520时程序输出1<BR>---------------"效率"最高的写法(只需一行代码)---------------<BR>puts
1314520.to_s(2).count('1')%2<BR>-----------------当然这里的效率是指程序员(人)的效率---------<BR>Ruby-lang<BR><IMG
style="MARGIN: 0px 0px -2px" alt="" src="位运算简介及实用技巧(二):进阶篇(1).files/Face_02.gif"
border=0><BR><BR><SPAN style="COLOR: red">回复:我越来越想学ruby了</SPAN></DIV></DIV>
<DIV class=comment>
<DIV class=commenttop><A
href="javascript:addQuote('Ai.Freedom','commcontent_893')" name=comm_893><IMG
style="MARGIN: 0px 4px -3px 0px" alt=""
src="位运算简介及实用技巧(二):进阶篇(1).files/icon_quote.gif" border=0></A> <A
href="http://aifreedom.com/" target=_blank><STRONG>Ai.Freedom</STRONG></A> <SPAN
class=commentinfo>[楼层: 10楼 发表时间: 2007-07-24 08:28 PM]</SPAN></DIV>
<DIV class=commentcontent id=commcontent_893>
<DIV class=UBBPanel>
<DIV class=UBBTitle><IMG style="MARGIN: 0px 2px -3px 0px" alt="引用来自 Ai.Freedom"
src="位运算简介及实用技巧(二):进阶篇(1).files/quote.gif"> 引用来自 Ai.Freedom</DIV>
<DIV class=UBBContent>引用来自 逆铭<BR>引用来自 dd<BR>n = n + 2; x = x <<
2;<BR>这是C程序员写出的代码吗?<BR>DD牛的意思应该是<BR>n+=2;x<<=2;<BR>这样写吧...<BR><BR>其实中间可以用逗号连接,这样连括号都省了...<BR><BR>这个我理解,
但这个的出处是哪里?</DIV></DIV><BR><BR>理解了, 原来是我习惯性地看东西跳过src带来的严重后果..</DIV></DIV>
<DIV class=comment>
<DIV class=commenttop><A
href="javascript:addQuote('Ai.Freedom','commcontent_888')" name=comm_888><IMG
style="MARGIN: 0px 4px -3px 0px" alt=""
src="位运算简介及实用技巧(二):进阶篇(1).files/icon_quote.gif" border=0></A> <A
href="http://aifreedom.com/" target=_blank><STRONG>Ai.Freedom</STRONG></A> <SPAN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -