﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-阵雨-随笔分类-Lua Programming</title><link>http://www.cppblog.com/zhenyu/category/362.html</link><description /><language>zh-cn</language><lastBuildDate>Mon, 19 May 2008 14:13:28 GMT</lastBuildDate><pubDate>Mon, 19 May 2008 14:13:28 GMT</pubDate><ttl>60</ttl><item><title>Programmming in Lua学习笔记(边学边更新ing。。。)</title><link>http://www.cppblog.com/zhenyu/archive/2005/11/17/1167.html</link><dc:creator>阵雨</dc:creator><author>阵雨</author><pubDate>Wed, 16 Nov 2005 18:30:00 GMT</pubDate><guid>http://www.cppblog.com/zhenyu/archive/2005/11/17/1167.html</guid><wfw:comment>http://www.cppblog.com/zhenyu/comments/1167.html</wfw:comment><comments>http://www.cppblog.com/zhenyu/archive/2005/11/17/1167.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/zhenyu/comments/commentRss/1167.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhenyu/services/trackbacks/1167.html</trackback:ping><description><![CDATA[<H1 align=center><FONT color=#0000ff><U>Programmming in Lua学习笔记</U></FONT></H1>
<P align=center>author:阵雨 2005年11月17日2:34:46</P>
<H1 align=left><FONT face=Tahoma><FONT color=#0000ff size=3>第0章 序言</FONT><BR></FONT></H1><FONT color=#0000ff>
<P><FONT face=Tahoma>
<HR>
</FONT>
<P></P>
<P></FONT><FONT face=Tahoma>Lua所提供的机制是C不善于的：高级语言、动态结构、简洁，易于测试和调试。<BR>正因为如此，Lua具有良好的安全保证，自动内存管理，简便的字符串处理功能<BR>以及其他动态数据的改变。<BR>Lua具有可扩展、简单、高效、跨平台的特点。</FONT></P>
<P><FONT face=Tahoma><FONT color=#0000ff><STRONG>第 1 章&nbsp; 起点</STRONG></FONT> 
<HR>
一个Chunk可以是一个语句，也可以是一系列语句的组合，还可以是函数，Chunk可以很大，在Lua中几个Mbyte的Chunk<BR>是很常见的。</FONT> 
<P></P>
<P><FONT face=Tahoma>全局变量不需要声明，给一个变量赋值后即创建了这个全局变量，访问一个没有初始值的全局<BR>变量也不会出错，只不过的得到的结果是:nil。<BR>如果想删除一个全局变量，只需要将变量赋值为 nil.<BR></FONT></P>
<P><FONT color=#0000ff><STRONG><BR><FONT face=Tahoma>第 2 章 类型和值</FONT></STRONG></FONT><FONT face=Tahoma> 
<HR>
每个语句结尾的分号 <FONT color=#0000ff>';'</FONT> 是可选的，但是如果同一行有多个语句最好用' ; '来将其隔开</FONT> 
<P></P>
<P><FONT face=Tahoma>Lua是动态类型语言，变量不要类型定义。lua中有8种基本类型分别为：</FONT></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><FONT face=Tahoma><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">nil&nbsp;,boolean&nbsp;,number,&nbsp;string,userdata,function,thread,table</SPAN></FONT></DIV>
<P><FONT face=Tahoma>变量没有预定义的类型，每一个变量都可能包含任一种类型的值，例如：</FONT></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><FONT face=Tahoma><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">print</SPAN><SPAN style="COLOR: #000000">(type(a))&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">--&gt;</SPAN><SPAN style="COLOR: #000000">nil&nbsp;(</SPAN><SPAN style="COLOR: #800000">'</SPAN><SPAN style="COLOR: #800000">a</SPAN><SPAN style="COLOR: #800000">'</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">is</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">not</SPAN></FONT><FONT face=Tahoma><SPAN style="COLOR: #000000">&nbsp;initialized)<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>a</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">10</SPAN></FONT><SPAN style="COLOR: #000000"><BR><FONT face=Tahoma><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></FONT></SPAN><FONT face=Tahoma><SPAN style="COLOR: #0000ff">print</SPAN><SPAN style="COLOR: #000000">(type(a))&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">--&gt;</SPAN></FONT><FONT face=Tahoma><SPAN style="COLOR: #000000">number<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>a</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">a&nbsp;string</SPAN><SPAN style="COLOR: #800000">"</SPAN></FONT><SPAN style="COLOR: #000000"><BR><FONT face=Tahoma><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></FONT></SPAN><FONT face=Tahoma><SPAN style="COLOR: #0000ff">print</SPAN><SPAN style="COLOR: #000000">(type(a))&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">--&gt;</SPAN></FONT><FONT face=Tahoma><SPAN style="COLOR: #000000">string<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>a</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #0000ff">print</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">--&gt;</SPAN><SPAN style="COLOR: #000000">yes&nbsp;,this&nbsp;</SPAN><SPAN style="COLOR: #0000ff">is</SPAN></FONT><FONT face=Tahoma><SPAN style="COLOR: #000000">&nbsp;valid!<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>a(type(a))&nbsp;</SPAN><SPAN style="COLOR: #000000">--&gt;</SPAN><SPAN style="COLOR: #000000">function</SPAN></FONT></DIV>
<P><FONT face=Tahoma>我们可以使用function像使用其它值一样使用</FONT></P>
<P><FONT face=Tahoma>Nil&nbsp;<BR>Lua中特殊的类型，它只有一个值:nil,一个全局变量没有被赋值以前默认值为nil</FONT></P>
<P><FONT face=Tahoma>Booleans 两个取值 <FONT color=#0000ff>false 和 true</FONT> ,但要注意Lua中 “所有的值”都可以用作为条件，在控制结构的<BR>条件中除了<FONT color=#0000ff>false 和 nil</FONT> 为假，其它值都为真，所以Lua认为<FONT color=#0000ff>0和空串</FONT>都是真.</FONT></P>
<P><FONT face=Tahoma>Lua的字符串可以包含任何数值的字符，包括嵌入的0，这意味着你可以存储任意的二进制数据在一个字符串里面。<BR>字符串是不可以修改的，你可以创建一个新的变量来存储你要的字符串<BR>Lua可以高效率的处理长字符串，1M大小的string在Lua中是常见的。<BR>可以用单引号或者双引号来表示字符串 <FONT color=#0000ff>a="a lien" ,b='another line'</FONT><BR>转义字符与C语言相同。<BR>可以在字符串中使用<FONT color=#0000ff>\ddd </FONT>(ddd为三位十进制数字) 方式来表示字母<BR>还可以使用<FONT color=#0000ff>[[ ...]]</FONT>来表示字符串，这种形式的字符串可以包含多行，也可以嵌套，<BR>而且不会解释转移字符，如果第一个字符是换行符会被自动忽略掉，这种形式的字符串<BR>用来包含一段代码是非常方便的<BR>运行时，Lua会自动在<FONT color=#0000ff>string</FONT>和<FONT color=#0000ff>numbers</FONT>之间自动进行类型转换，当一个字符串使用算术<BR>操作符时，string会被转换成数字<BR>尽管字符串和数字可以自动转换，但是两者是不同的，比如 </FONT><FONT face=Tahoma><FONT color=#0000ff>10 ~= "10".<BR></FONT><BR>Lua通过引用比较tables userdata functions ,也就是说当且仅当两者表示同一个对象时<BR>他们才相同，nil只和nil相等。如果两个值类型不同，Lua则认为他们不同</FONT></P>
<P><BR><FONT face=Tahoma><FONT color=#0000ff><STRONG>第 3 章 表达式<BR>
<HR>
</STRONG></FONT>and 和 or 的运算结果不是true或者false,而是和它的两个操作数相关.<BR>a and b&nbsp; a or b&nbsp;&nbsp;按照C语言的逻辑运算顺序计算，a、b两个表达式，计算到哪一个就返回哪一个的值<BR>例如：a &amp;&amp; b&nbsp;如果 a为false,那么无须计算b 即可知表达式为false<BR>所以&nbsp;a and b的结果是如果a为false,则返回a,否则返回b</FONT> 
<P></P>
<P><FONT face=Tahoma>一个很实用的技巧，<FONT color=#0000ff>x = x or v</FONT><BR>C语言中的 <FONT color=#0000ff>a ? b:c</FONT>&nbsp;等价于Lua中的&nbsp;<FONT color=#0000ff>(a and b) or c</FONT></FONT></P>
<P><FONT face=Tahoma>运算符的优先级(可惜没有位运算<IMG height=19 src="http://www.cppblog.com/Emoticons/cry_smile.gif" width=19 border=0>)：</FONT></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><FONT face=Tahoma><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">^</SPAN></FONT><SPAN style="COLOR: #000000"><BR><FONT face=Tahoma><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;</FONT></SPAN><FONT face=Tahoma><SPAN style="COLOR: #0000ff">not</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">-</SPAN></FONT><FONT face=Tahoma><SPAN style="COLOR: #000000">(unary)<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">/</SPAN></FONT><SPAN style="COLOR: #000000"><BR><FONT face=Tahoma><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;</FONT></SPAN><FONT face=Tahoma><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">-</SPAN></FONT><SPAN style="COLOR: #000000"><BR><FONT face=Tahoma><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;..<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;</FONT></SPAN><FONT face=Tahoma><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&gt;=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">~=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN></FONT><SPAN style="COLOR: #000000"><BR><FONT face=Tahoma><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;</FONT></SPAN><SPAN style="COLOR: #0000ff"><FONT face=Tahoma>and</FONT></SPAN><SPAN style="COLOR: #000000"><BR><FONT face=Tahoma><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;</FONT></SPAN><SPAN style="COLOR: #0000ff"><FONT face=Tahoma>or</FONT></SPAN></DIV>
<P><FONT face=Tahoma>除了<FONT color=#0000ff>^和 ..</FONT> 之外所有的二元运算符都是左连接的。</FONT></P>
<P><FONT face=Tahoma>Lua中的表可以用字符串和数字作为<FONT color=#0000ff>index,如tt={name="table",["size"]=2，[3]="number"}</FONT><BR>如果不使用数字表达式，Lua则自动从数字 1 开始作为index,如<FONT color=#0000ff>tt={"table",2}</FONT> 那么<FONT color=#0000ff>tt[1]=="table,tt[2]==2</FONT><BR>在构造函数中,域分隔符逗号(,)是可以用分号(;)替代的，通常我们用分号来分割不同类型的表元素。如：<BR><FONT color=#0000ff>tt={x=10,y=45 ; "one","two","three"}<BR></FONT><FONT color=#ff0000>The syntax var.NAME is just syntactic sugar for var["NAME"]:<BR></FONT></FONT></P><img src ="http://www.cppblog.com/zhenyu/aggbug/1167.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhenyu/" target="_blank">阵雨</a> 2005-11-17 02:30 <a href="http://www.cppblog.com/zhenyu/archive/2005/11/17/1167.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Lua实现的 位运算 模块</title><link>http://www.cppblog.com/zhenyu/archive/2005/11/11/1050.html</link><dc:creator>阵雨</dc:creator><author>阵雨</author><pubDate>Thu, 10 Nov 2005 19:08:00 GMT</pubDate><guid>http://www.cppblog.com/zhenyu/archive/2005/11/11/1050.html</guid><wfw:comment>http://www.cppblog.com/zhenyu/comments/1050.html</wfw:comment><comments>http://www.cppblog.com/zhenyu/archive/2005/11/11/1050.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/zhenyu/comments/commentRss/1050.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhenyu/services/trackbacks/1050.html</trackback:ping><description><![CDATA[<FONT face="Courier New"><FONT color=#008000>--[[<BR>Description:<BR>&nbsp;&nbsp;&nbsp;&nbsp;FileName:bit.lua<BR>&nbsp;&nbsp;&nbsp;&nbsp;This&nbsp;module&nbsp;provides&nbsp;a&nbsp;selection&nbsp;of&nbsp;bitwise&nbsp;operations.<BR>History:<BR>&nbsp;&nbsp;&nbsp;&nbsp;Initial&nbsp;version&nbsp;created&nbsp;by&nbsp;&nbsp;阵雨&nbsp;2005-11-10.<BR>Notes:<BR>&nbsp;&nbsp;....<BR>]]<BR>--[[{2147483648,1073741824,536870912,268435456,134217728,67108864,33554432,16777216,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;8388608,4194304,2097152,1048576,524288,262144,131072,65536,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;32768,16384,8192,4096,2048,1024,512,256,128,64,32,16,8,4,2,1}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]]<BR></FONT><BR><BR>bit={data32={}}<BR><FONT color=#0000ff>for</FONT>&nbsp;i=1,32&nbsp;<FONT color=#0000ff>do</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;bit.data32[i]=2^(32-i)<BR><FONT color=#0000ff>end</FONT><BR><BR><FONT color=#0000ff>function</FONT>&nbsp;bit:d2b(arg)<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>local</FONT>&nbsp;&nbsp;&nbsp;tr={}<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>for</FONT>&nbsp;i=1,32&nbsp;<FONT color=#0000ff>do</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>if</FONT>&nbsp;arg&nbsp;&gt;=&nbsp;self.data32[i]&nbsp;<FONT color=#0000ff>then</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tr[i]=1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arg=arg-self.data32[i]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>else</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tr[i]=0<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>end</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>end</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>return</FONT>&nbsp;&nbsp;&nbsp;tr<BR><FONT color=#0000ff>end&nbsp;&nbsp;&nbsp;</FONT><FONT color=#008000>--bit:d2b</FONT><BR><BR><FONT color=#0000ff>function</FONT>&nbsp;&nbsp;&nbsp;&nbsp;bit:b2d(arg)<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>local</FONT>&nbsp;&nbsp;&nbsp;nr=0<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>for</FONT>&nbsp;i=1,32&nbsp;<FONT color=#0000ff>do</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>if</FONT>&nbsp;arg[i]&nbsp;==1&nbsp;<FONT color=#0000ff>then</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nr=nr+2^(32-i)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>end</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>end</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>return</FONT>&nbsp;&nbsp;nr<BR><FONT color=#0000ff>end</FONT>&nbsp;&nbsp;&nbsp;<FONT color=#008000>--bit:b2d</FONT><BR><BR><FONT color=#0000ff>function</FONT>&nbsp;&nbsp;&nbsp;&nbsp;bit:_xor(a,b)<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>local</FONT>&nbsp;&nbsp;&nbsp;op1=self:d2b(a)<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>local</FONT>&nbsp;&nbsp;&nbsp;op2=self:d2b(b)<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>local</FONT>&nbsp;&nbsp;&nbsp;r={}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>for</FONT>&nbsp;i=1,32&nbsp;<FONT color=#0000ff>do</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>if</FONT>&nbsp;op1[i]==op2[i]&nbsp;<FONT color=#0000ff>then</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r[i]=0<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>else</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r[i]=1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>end</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>end</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>return</FONT>&nbsp;&nbsp;self:b2d(r)<BR><FONT color=#0000ff>end</FONT>&nbsp;<FONT color=#008000>--bit:xor</FONT><BR><BR><FONT color=#0000ff>function</FONT>&nbsp;&nbsp;&nbsp;&nbsp;bit:_and(a,b)<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>local</FONT>&nbsp;&nbsp;&nbsp;op1=self:d2b(a)<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>local</FONT>&nbsp;&nbsp;&nbsp;op2=self:d2b(b)<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>local</FONT>&nbsp;&nbsp;&nbsp;r={}<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>for</FONT>&nbsp;i=1,32&nbsp;<FONT color=#0000ff>do</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>if</FONT>&nbsp;op1[i]==1&nbsp;<FONT color=#0000ff>and</FONT>&nbsp;op2[i]==1&nbsp;&nbsp;<FONT color=#0000ff>then</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r[i]=1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>else</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r[i]=0<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>end</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>end</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>return</FONT>&nbsp;&nbsp;self:b2d(r)<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR><FONT color=#0000ff>end</FONT>&nbsp;<FONT color=#008000>--bit:_and</FONT><BR><BR><FONT color=#0000ff>function</FONT>&nbsp;&nbsp;&nbsp;&nbsp;bit:_or(a,b)<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>local</FONT>&nbsp;&nbsp;&nbsp;op1=self:d2b(a)<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>local</FONT>&nbsp;&nbsp;&nbsp;op2=self:d2b(b)<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>local</FONT>&nbsp;&nbsp;&nbsp;r={}<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>for</FONT>&nbsp;i=1,32&nbsp;<FONT color=#0000ff>do</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>if</FONT>&nbsp;&nbsp;op1[i]==1&nbsp;<FONT color=#0000ff>or</FONT>&nbsp;&nbsp;&nbsp;op2[i]==1&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>then</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r[i]=1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>else</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r[i]=0<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>end</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>end</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>return</FONT>&nbsp;&nbsp;self:b2d(r)<BR><FONT color=#0000ff>end</FONT>&nbsp;<FONT color=#008000>--bit:_or</FONT><BR><BR><FONT color=#0000ff>function</FONT>&nbsp;&nbsp;&nbsp;&nbsp;bit:_not(a)<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>local</FONT>&nbsp;&nbsp;&nbsp;op1=self:d2b(a)<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>local</FONT>&nbsp;&nbsp;&nbsp;r={}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>for</FONT>&nbsp;i=1,32&nbsp;<FONT color=#0000ff>do</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>if</FONT>&nbsp;&nbsp;op1[i]==1&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>then</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r[i]=0<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>else</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r[i]=1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>end</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>end</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>return</FONT>&nbsp;&nbsp;self:b2d(r)<BR><FONT color=#0000ff>end</FONT>&nbsp;<FONT color=#008000>--bit:_not</FONT><BR><BR><FONT color=#0000ff>function</FONT>&nbsp;&nbsp;&nbsp;&nbsp;bit:_rshift(a,n)<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>local</FONT>&nbsp;&nbsp;&nbsp;op1=self:d2b(a)<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>local</FONT>&nbsp;&nbsp;&nbsp;r=self:d2b(0)<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>if</FONT>&nbsp;n&nbsp;&lt;&nbsp;32&nbsp;<FONT color=#0000ff>and</FONT>&nbsp;n&nbsp;&gt;&nbsp;0&nbsp;<FONT color=#0000ff>then</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>for</FONT>&nbsp;i=1,n&nbsp;<FONT color=#0000ff>do</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>for</FONT>&nbsp;i=31,1,-1&nbsp;<FONT color=#0000ff>do</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;op1[i+1]=op1[i]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>end</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;op1[1]=0<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>end</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;r=op1<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>end</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>return</FONT>&nbsp;&nbsp;self:b2d(r)<BR><FONT color=#0000ff>end</FONT>&nbsp;<FONT color=#008000>--bit:_rshift</FONT><BR><BR><FONT color=#0000ff>function</FONT>&nbsp;&nbsp;&nbsp;&nbsp;bit:_lshift(a,n)<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>local</FONT>&nbsp;&nbsp;&nbsp;op1=self:d2b(a)<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>local</FONT>&nbsp;&nbsp;&nbsp;r=self:d2b(0)<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>if</FONT>&nbsp;n&nbsp;&lt;&nbsp;32&nbsp;<FONT color=#0000ff>and</FONT>&nbsp;n&nbsp;&gt;&nbsp;0&nbsp;<FONT color=#0000ff>then</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>for</FONT>&nbsp;i=1,n&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>do</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>for</FONT>&nbsp;i=1,31&nbsp;<FONT color=#0000ff>do</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;op1[i]=op1[i+1]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>end</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;op1[32]=0<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>end</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;r=op1<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>end</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>return</FONT>&nbsp;&nbsp;self:b2d(r)<BR><FONT color=#0000ff>end</FONT>&nbsp;<FONT color=#008000>--bit:_lshift</FONT><BR><BR><BR><FONT color=#0000ff>function</FONT>&nbsp;&nbsp;&nbsp;&nbsp;bit:print(ta)<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>local</FONT>&nbsp;&nbsp;&nbsp;sr=<FONT color=#ff00ff>""</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>for</FONT>&nbsp;i=1,32&nbsp;<FONT color=#0000ff>do</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sr=sr..ta[i]<BR>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>end</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;print(sr)<BR><FONT color=#0000ff>end</FONT><BR><BR>bs=bit:d2b(7)<BR>bit:print(bs)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR><FONT color=#008000>--&gt;00000000000000000000000000000111</FONT><BR>bit:print(bit:d2b(bit:_not(7)))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR><FONT color=#008000><FONT color=#000000>--</FONT>&gt;11111111111111111111111111111000</FONT><BR>bit:print(bit:d2b(bit:_rshift(7,2)))&nbsp;&nbsp;&nbsp;&nbsp;<BR><FONT color=#008000><FONT color=#000000>--</FONT>&gt;00000000000000000000000000000001<BR></FONT>bit:print(bit:d2b(bit:_lshift(7,2)))&nbsp;&nbsp;&nbsp;&nbsp;<BR><FONT color=#008000><FONT color=#000000>--</FONT>&gt;00000000000000000000000000011100</FONT><BR>print(bit:b2d(bs)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#008000>--&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7</FONT><BR>print(bit:_xor(7,2))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#008000>--&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5</FONT><BR>print(bit:_and(7,4))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#008000>--&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4</FONT><BR>print(bit:_or(5,2))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#008000>--&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7</FONT><BR><BR><BR><FONT style="BACKGROUND-COLOR: #ffffff" color=#008000>--end&nbsp;of&nbsp;bit.lua</FONT></FONT><img src ="http://www.cppblog.com/zhenyu/aggbug/1050.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhenyu/" target="_blank">阵雨</a> 2005-11-11 03:08 <a href="http://www.cppblog.com/zhenyu/archive/2005/11/11/1050.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>