SMALL语言基础
译者:jhting
声明:本文只是本人为学习计算机英语而练习的,为了以后的程序员考试,虽然最讨厌的是英语,但还是要了解了解吧!,如有错误多多指教。
未完待续:
变量:
Small is an embeddable, (almost) typeless, easy to use scripting language that is compiled for a virtual machine. AMX Mod X uses Small to route scripting functions to the Half-Life engine, using the Small Virtual Machine and Metamod (Small is written in C, Metamod is written in C++). While you write Small scripts in a text editor, the scripts must be compiled with a "Compiler", which produces a binary for AMX Mod X. The AMX Mod X team distributes a specially modified Small compiler.
Small是一种可可嵌入的语言(几乎所有的类型)、很容易用脚本语言在一台虚拟机上编译实现的脚本语言。AMX Mod X 用SMALL语言发送脚本函数到HL引擎,利用SMALL虚拟计算机和Metamod(SMALL是用C写的,Metamod用C++),当你写一SMALL脚本在一个文本编辑器中,脚本必须被编译用一个编译器,以产生二进制代码。AMX MOD X组开发了一种特别的修改器--Small编译器。
Programming scripts in Small is relatively easy, and does not have concepts in other languages that are unnecessary for general use, such as pointers, vectors, structs, classes, streams, et cetera. Small has just three data types for declaring variables. The default variable type is a regular whole number, or integer. A variable name, for backwards compatibility, should be 19 characters or less, and MUST start with a letter. It can contain the symbols A-Z, a-z, 0-9, and the underscore ("_"). It is important to note that variable names are case sensitive - "myvar", "MyVaR", and "MYVAR" are three separate symbols. To declare a new variable, use the "new" operator like so:
用SMALL写脚本语言相当的简单,并且没有多余的概念,如指针,向量,结构体,类,串等等。Small只用三种数据类型。在定义变量是若不指定数据类型时默认的是整型,一个变量的名字为了向后兼容,应该不要多于19个字符,而且必须以字母开始。它可以是字母、数字、和下划线。必须注意的是SMALL是区分大小写的"myvar","MyVaR",及"MYVAR"是三个不同的变量,如为了定义一个个新的变量,用“new"语句,如:
new a //Declare empty variable "a" 定义了一个变量a,值为空值。
new b=5 //Declare variable "b" and set it to 5. 定义了一个变量b,值为整型5。
new c=5.0 //This is invalid, technically not a whole number! 这样定义是错误的,因为没有指定数据类型而默认为整型,而赋的值却是实型
new d="hello" //"hello" is not a number either, this is invalid.
//"hello"不是一个整数值,这也是错误的
//You can also declare multiple variables on one line:
//你也可以这样定义多个变量在一个语句中
new e,f,g,h
new x=7, y=3
You can also declare a variable as a "Float", which means it can store numbers with decimal places. These are called "floating point" numbers:
你也可以定义一个实型变量,这就意味着它能存储小数位。他也叫做浮点数
new Float:a //Declare empty floating point variable "a"
new Float:b=5.3 //This will declare a new variable "b" and assign 5.3 to it.
new Float:c=5 //This is valid(正确的), but the compiler will give you a warning.(但会给以警告)
new Float:d="hello" //This is invalid(错误), "hello" is not a decimal number.
You can also do the following:
你也可以如下
//float(n) is a function that takes a number n and makes it a
//float(n)是一个带了一个参数的函数
// floating point number.
new Float:var = float(5)
new Float:var2 = 5.0
new Float:var3 = 1.0*5
new var4 = floatround(5.0)
//Note: floatround(n) is a function that takes a number n and rounds it to a whole number.
//注意:函数floatround(n)是把n转为一个整数
// this makes the assignment to a regular integer variable valid.
Note - Spacing does generally not matter, as long as the compiler can tell symbols apart from each other. If your spacing is REALLY bad, you will get errors or maybe even warnings. For example, "new var = 5" and "new var=5" are the same, but "newvar=5" is totally wrong.
注意 - 空格一般不会被当作内容,同样这编译器能告诉来自其它的分离字符。如果你的空格会错识,你可能被编译器通知出错或是一个警告。如,"new var = 5"和 "new var=5"是一样的,但是"newvar=5"是完全的错了。
The last variable type is "boolean". It is very simple - it is either "true", or "false". Both "true" and "false" are predefined data structures.
最后介绍的一个变量类型是布尔型(boolean),它是非常简单的一种类型,它的值只有"true"或"false"。"true"和"false"是预先被定义的数据结构
new bool:IsItOn //Declares a new variable "IsItOn" which is automatically false
//定义一个新的变量IsItOn:boolean值是false
new bool:xyz=true //Declares a new variable "xyz" set to true
//定义一个新的变量xyz:boolean值是true
数组
Small features basic "arrays". An array is a very important type of aggregate data. This means you can store multiple values in one variable! An array follows the same rules as a regular variable, and it has the same types. It simply can contain multiple values. You define an array with brackets, and how many values it can hold.
数组在SMALL语言中是一个特别重要的数据类型。数组从它的名字来看就意味着你能够存贮一批数据!数组同样遵循变量的命名规则,并且数组中的数据类型是一种类型。数组简单的把一批数据包容在一起。定义数组时用一对[]来定认数组的大小。
For example:
//This will declare a variable called "Players" which holds 32 numbers.
//这里定义了一个名叫做Players大小为32的数组。
new Players[32]
//You can now store values in any of the 32 "slots" this array has.
//现在你能够存贮数据在这分配的32个空间中。
// The slots are numbered from 0 to n-1, or in this case, 0 to 31.
//数组是从0到N-1,所以数组大小为N-1-0+1,
//Every slot starts off as 0.
//Set slot 0 to 5
Players[0] = 5
//Set slot 1 to whatever is in slot 0, in this case, the number 5
Players[1] = Players[0]
//This is invalid!
//这样是错误的
//Although there are 32 slots, they are numbered from 0 to 31.
//虽然有32个数组元素,但是界限是从0到31。
//Doing this results in AMX Native Error 4 - AMX_ERR_BOUNDS
//这样写的结果是,提示错误越界。
// or, it simply won't compile!
//这仅仅是编译错误
Players[32] = 15
//This is also totally invalid
//这是完全错误的
Players[-1] = 6
//This is also totally invalid.
//这一样是错误的
//a must be a constant number, so this is valid:
//必须是一个常量,以下是正确的
new a = 3
new BadArray[a]
const b = 3
new GoodArray
//You can also use Compiler Directives (See last section)
#define ARRAY_SIZE 3
//宏定义
new Array[ARRAY_SIZE]
(对于学过C的朋友是太简相似了)
Arrays can also be declared with groups of data default, such as:
数组也可以整体赋值:
new Numbers[4]={0,1,2,3}
//Note: it is important that you make sure the amount of numbers you pass and the size of the array
//注意:{}里的数值的个数要不要大于数组的最大容量。
You can also use any data type
with arrays:
当然数组也有不同的类型(默认的同变量一样是整型):
//Array of floating points:
//定义一个实型数组:
new Float:Numbers[4] = {0.0, 1.2, 2.4, 3.8}
//Array of booleans. Note this sets every slot to true.
//定义布尔型数组。特加注意以下的这条语句定义了数组中的每一个元素都为真
new bool:playerHasGun[32] = true
(这和其它的不同要加以注意)
字符串
You have probably noticed that an important data type is missing - characters (letters and symbols). These are called "strings", and in Small, they are technically numbers! A string of data is an array of numbers that translate to ASCII (character) symbols. For example:
你可能已经注意到另外一种不可缺少的数据类型-字母和字符。他们都称为字符串在SMALL语言中,一个字符串数据就是一个能被翻译成ASCII码的字符的数值数组。列如
//This will declare a number array "myString" that contains the data "Hello".
//这定义了一个字符数组“myString",在这个数组中包含字符串"hello".
//It will have 6 slots, because there are 5 characters.
//数组中有6个元素,因为他有5个字符。(WHY!!)
//The last slot is reserved for the number 0, which tells the Small engine that it is a string.
//这最后一个位置被用来接收ASCII码0,(ASCII码0是不做任何操作的,同C(C中转意字符\0),只是字符结束的一个标志)这里告诉SMALL编译器这是一个字符串。
new myString[] = "Hello"
//Note: anything in between /* and */ is also a comment. You cannot use /* */ inside a /* */.
//注意:/*和*/之间的任何代码都是注释,但注释符/**/不能嵌套使用。
/* The following set of commands achieves the same purpose, however, it is longer and not recommended.
/* 以下语句可以达到相同的目的,无论如何,它是很久以前的和不被推荐的。
This works because each character of the string "Hello" is stored in a slot in the array。
以下语句的功能是把字符串"hello"中的每一个字符按顺序的存贮在数组中。*/
new myString[6]
myString[0] = "H"
myString[1] = "e"
myString[2] = "l"
myString[3] = "l"
myString[4] = "o"
myString[5] = 0
/*You CANNOT do this! While it may compile, it is highly dangerous as it might cause overflow errors.*/
/*你不能这样做,便编译它时,很可能出现溢出错误*/
new myString[6]
myString = "Hello" //INVALID!(错误的)
myString[0] = "Hello" //INVALID!(错误的)
//To add data to a string, you can do this:
//要为一个字符串赋值,你可以这样做
new goodString[6]
copy(goodString, 6, "Hello")
//Copy is a function that takes three parameters:
//Copy是一个函数,它有三个参数(以下是它的函数原形):
copy(destination[], length, source[])
//It copies the string inside the source array and places 。it into the destination array,
//它的作用是把源字符数组(source[])中的字符拷贝到目的字符数组(destination[])中去。
// but only copies up to length characters.
//但是只能拷贝目的字符串能容纳下的字符
//Lastly, to prove that a string is really an array of numbers, this is completely valid:
//最后为了,表明字符串是一个真正的数值数组,我们给出以下一个很好的有效的依据!
new weird[6]
weird[0] = 68
weird[1] = 65
weird[2] = 73
weird[3] = 86
weird[4] = 68
weird[5] = 0
//This will set the variable "weird" to the string "DAVID".
//这里设置变量weird是一个字符串数组,它的值是"DAVID"
//To see how letters and symbols translate into numbers, visit www.asctiitable.com
//如果你想知道怎样把字母和字符翻译成数值(ASCII)码,那么请访问这www.asctiitable.com
译者:在这里我行可能更清楚的看到字符数组的未为么要给0值的原因了,不这样做的结果是weird[6]会成为一个整型数组了
(译者附:ASCII码是美国ANSI标准委员会制定的一种字符编码!以下例出几个常见的ASCII码
[SIZE=+0]0—9:
48-57
A-z:
65—90
[SIZE=+0]a-z:
97-122
0
NULL
13:
CR(carriage return)回行首
10:
LF(line feed) 换行
)
在网上找了一下找到一张表,还是发出来吧,很方便的,不但有十进制也有八进制的和十六进制的!
Table of ASCII CharactersThis table lists the ASCII characters and their decimal, octal and hexadecimal numbers. Characters which appear as names in parentheses (e.g., (nl)) are non-printing characters. A table of the common non-printing characters appears after this table.
Char Dec Oct Hex | Char Dec Oct Hex | Char Dec Oct Hex | Char Dec Oct Hex
-------------------------------------------------------------------------------------
(nul) 0 0000 0x00 | (sp) 32 0040 0x20 | @ 64 0100 0x40 | ` 96 0140 0x60
(soh) 1 0001 0x01 | ! 33 0041 0x21 | A 65 0101 0x41 | a 97 0141 0x61
(stx) 2 0002 0x02 | " 34 0042 0x22 | B 66 0102 0x42 | b 98 0142 0x62
(etx) 3 0003 0x03 | # 35 0043 0x23 | C 67 0103 0x43 | c 99 0143 0x63
(eot) 4 0004 0x04 | $ 36 0044 0x24 | D 68 0104 0x44 | d 100 0144 0x64
(enq) 5 0005 0x05 | % 37 0045 0x25 | E 69 0105 0x45 | e 101 0145 0x65
(ack) 6 0006 0x06 | & 38 0046 0x26 | F 70 0106 0x46 | f 102 0146 0x66
(bel) 7 0007 0x07 | ' 39 0047 0x27 | G 71 0107 0x47 | g 103 0147 0x67
(bs) 8 0010 0x08 | ( 40 0050 0x28 | H 72 0110 0x48 | h 104 0150 0x68
(ht) 9 0011 0x09 | ) 41 0051 0x29 | I 73 0111 0x49 | i 105 0151 0x69
(nl) 10 0012 0x0a | * 42 0052 0x2a | J 74 0112 0x4a | j 106 0152 0x6a
(vt) 11 0013 0x0b | + 43 0053 0x2b | K 75 0113 0x4b | k 107 0153 0x6b
(np) 12 0014 0x0c | , 44 0054 0x2c | L 76 0114 0x4c | l 108 0154 0x6c
(cr) 13 0015 0x0d | - 45 0055 0x2d | M 77 0115 0x4d | m 109 0155 0x6d
(so) 14 0016 0x0e | . 46 0056 0x2e | N 78 0116 0x4e | n 110 0156 0x6e
(si) 15 0017 0x0f | / 47 0057 0x2f | O 79 0117 0x4f | o 111 0157 0x6f
(dle) 16 0020 0x10 | 0 48 0060 0x30 | P 80 0120 0x50 | p 112 0160 0x70
(dc1) 17 0021 0x11 | 1 49 0061 0x31 | Q 81 0121 0x51 | q 113 0161 0x71
(dc2) 18 0022 0x12 | 2 50 0062 0x32 | R 82 0122 0x52 | r 114 0162 0x72
(dc3) 19 0023 0x13 | 3 51 0063 0x33 | S 83 0123 0x53 | s 115 0163 0x73
(dc4) 20 0024 0x14 | 4 52 0064 0x34 | T 84 0124 0x54 | t 116 0164 0x74
(nak) 21 0025 0x15 | 5 53 0065 0x35 | U 85 0125 0x55 | u 117 0165 0x75
(syn) 22 0026 0x16 | 6 54 0066 0x36 | V 86 0126 0x56 | v 118 0166 0x76
(etb) 23 0027 0x17 | 7 55 0067 0x37 | W 87 0127 0x57 | w 119 0167 0x77
(can) 24 0030 0x18 | 8 56 0070 0x38 | X 88 0130 0x58 | x 120 0170 0x78
(em) 25 0031 0x19 | 9 57 0071 0x39 | Y 89 0131 0x59 | y 121 0171 0x79
(sub) 26 0032 0x1a | : 58 0072 0x3a | Z 90 0132 0x5a | z 122 0172 0x7a
(esc) 27 0033 0x1b | ; 59 0073 0x3b | [ 91 0133 0x5b | { 123 0173 0x7b
(fs) 28 0034 0x1c | < 60 0074 0x3c | \ 92 0134 0x5c | | 124 0174 0x7c
(gs) 29 0035 0x1d | = 61 0075 0x3d | ] 93 0135 0x5d | } 125 0175 0x7d
(rs) 30 0036 0x1e | > 62 0076 0x3e | ^ 94 0136 0x5e | ~ 126 0176 0x7e
(us) 31 0037 0x1f | ? 63 0077 0x3f | _ 95 0137 0x5f | (del) 127 0177 0x7f
ASCII Name Description C Escape Sequence nul null byte \0 bel bell character \a bs backspace \b ht horizontal tab \t np formfeed \f nl newline \n cr carriage return \r vt vertical tab esc escape sp space
函数
Small allows you to define your own functions. This comes in handy for removing code that is used in multiple places. Note that all functions should return a value. To do this, you use the "return" command, which immediately halts the function and returns the value of the expression passed to it. No code is executed in a function once the return is found. Here are some examples
Samll语言允许你定义自己的函数,这样带来的好外是提高了代码的编写效率,可以重复使用,注意所有的函数都应该有返因值。为了达实玩这个,你可以用return语句,这个语句有两个功能:一:立即停止函数;二:返回表达式已经传递给它的一个值。一旦出现了return语句-在函数中,return以后的语句就不会被执行了,这有一些例子。
//This is a function that takes no parameters and returns 1.
//这是一个无参函数,返回值是1。//When activated, it uses the (non-existant) print function.
//当被引用时,它用作一个打印函数。
show()
{
print("Hello!")
return 1 //End, return 1
}
//Activate like this:
//可以这样引用它:
show()
You can also declare functions to take parameters.
你也可以定义带有参数的函数。
//This declares a function called "add_two_numbers", which takes two numbers and returns the sum.
//这定义了一个名叫"add_two_numbers"的函数,它带有两个参数,并且返因一个值-sum
add_two_numbers(first, second)
{
new sum = first + second
return sum //Return the sum
}
//Then you can use your new function like this:
//当你想用这个你自己定义的函数时可以这样引用它:
new a,b
a = 5
a = 12
new c = add_two_numbers(a,b)
//c will now be equal to 17.
//变量C会被赋予值17。
You are not limited by what types of data parameters can accept. When you give parameters to a function, it is called "passing". You can pass either data or a variable to a function.
一个函数可以接收任意一种数据类型的数据(当然是SMALL所允许的)。当你给一参数给一函数时,这个过程被称为“传递”(译者:不知这样说对不对)。你可以传递任何数据和变量给函数。
//This defines a new function called "add_two_floats"
//这定义了一个新的函数名字是“add_two_floats"// which takes two floating points and returns the sum
//功能是返回求两个实数的和并返回这个和。
Float:add_two_floats(Float:first, Float:second)
{
new Float:sum = first + second
return sum
}
new Float:a
new Float:b
a = 5.0
b = 6.3
new Float:c
c = add_two_floats( a+b )
//c is now equal to 11.3
//C被赋值11.3
//You can even pass arrays! You do not have to specify the size of the array.
//我们也可以用数组来做为一个函数的形参!你不必特别指定数组的大小。
(译者:用数组去做参数是时,编译器不会去检测数组的大小,在其它的变量做参数时,你甚至可以不用写形参的变量名,
因为编译器不会去检测,它只检测数据类型和参数个数)
//If you do, you must make sure you are calling the function with an array of equal size and type.
//如果你用一个数组做参数,在你调用这个函数时你一定要确定你传递给函数的数组的大小和类型。
add_two_from_array(array[], a, b)
{
new first = array[a]
new second = array
new sum = add_two_numbers(first, second) //use our function from earlier
return sum
}
Note, that when you pass arrays through a function they are passed through what is called "by reference". When a normal variable is passed to a function, it is copied in memory, and the copy is sent and then deleted afterwards. This is not the case with an array. Because arrays can be very large, the array is "referenced" instead of copied. This means if you change the array, afterwards it will stay changed. For example:
注意,当你传递一个数组通过一个函数调用那么这个数组被称为“形参”。当一个普通的变量被传递到一个函数,那们它会被写进存储器,同时被复制然后删除。这是和数组所不同的。因为数组可能很大,所以数组改为"引用"代替"拷贝".意思是如果你改变了数组(在函数中),那么你的数组将会改变(在main()函数中,而普通变量则不会)例如:
//This function will switch slots a and b inside any array passed to this function.
//这个函数将交换a和b的位置,swap_slots(array[], a, b){ new temp //Note, you need to temporarily hold one of the slots before swapping them
//注意,你需要用一个变量临时的保存其中的一个值,在交换之前 //Otherwise, you can't swap both values! This is a classic problem.
//另外,你不能交换两个变量的值!这是一个很值得注意的问题。 //If you have a and b, setting b equal to a eliminates the original value in b.
//如果你有两个变量a、b,把b的值赋给a清除原来的值。 temp = array array = array[a] array[a] = temp}new myArray[2]myArray[0] = 5myArray[1] = 6swap_slots(myArray, 0, 1)//myArray[0] is 6, myArray[1] is 5//You can prevent arrays from being modified by declaring them "constant", like so:
//你可以防止数组被修改用"constant"如下add_two_from_array(const array[], a, b){ new first = array[a] new second = array new sum = add_two_from_array(first, second)一步 //(译者:一个递归调用) return sum}//Note, now when you use the function, you are guaranteed that the array will not be modified.
//注意,当你用这个函数,你保证了函数不会被修改。//This function modifies an array passed as a constant. It will not work.
//这个函数试图修改传递过来的一个数组,但它是实现不了的,因为用了语句constbad_function(const array[]){ array[0] = 0}
表达式
Expressions are just what they sound like from mathematics. They are groupings of symbols that return one piece of data. Expressions are normally comprised of parenthetical expressions, and are evaluated in a certain order (from innermost to outermost, parenthesis first, then multiplication, division, addition, subtraction, et cetera). You can put expressions anywhere. You can set variables equal to them or pass them to functions.
表达式就是数学的语言,他们是返回一数据的字符组合(译得想吐),表达式通常是由附加说明的(郁闷)和被求的值在一个命令(来自圆括号里的、乘法、除法、加法、减法等)。你可以输出表达式的值到任何一个地方,你可以把它的值直接赋给变量,或传递给一个函数
//This is the simplest expression. It returns the number zero.
//这也是一个简单不过的表达式了,他返回一个值0。
0
//However, to make it easier to read, this is also valid:
//不管怎样,为了易读,这样也是对的
(0)
If an expression is not zero or it is not false, it not only returns a value, it also returns "true". Otherwise, it will return 0, which is also "false".
如果一个表达式不是零或他不是false,它不仅仅是返回一个值,它也返回"true"。别外它返回0也就是"false"
//Here are more mathematical expressions. The mathematical operators are
//这有一些数学表达式,这操作是:
// + for addition
//求和
// - for subtraction
//作差
// * for multiplication
//乘积
// / for division
//除法
// % for modulus (finding the remainder of one number divided by another (5%2 is 1)
//求模
(5+6) //returns 11 返回11
((5*6)+3) //returns 33
((((5+3)/2)*4)-9) //returns 5
((5*6) % 7) //returns 2
//Here are other expressions:
//这有一些其它的表达式
(true) //returns true
(5.0 + 2.3) //returns 7.3 as a floating point(返回的是浮点数)
//There are also extensions of these operators for direct use on variables.
//这同样也有一些扩展,变量与表达式接合
new a = 5
new b = 6
//The first are the post/pre increment and decrement operators.
//自增和自减
a++ //returns a+1, or 6. This is a post increment.(首先用a,再使变量加1)
++a //also returns a+1, or 6. This is a pre increment.(同样要使a加1,但是加1后再用变量a)
The difference between the two is subtle but important. a++ is evaluated LAST in an expression, while ++a is evaluated FIRST. This differences comes in handy with code that uses loops in certain ways. It is also important to know that the increment/decrement operators will not only return a+1, but set the variable a to a+1.
这样的差别很上但是很重要,a++是先在一个表达式中求值,再加+1,++a则相反。这小小的差别在程序上带来很大的方便在循环语句中。它也是很重要的为了知道增量减量运算将不仅仅是返回a+1,但使变量a变成a+1
a-- //returns 4, post decrement
--a //returns 4, pre decrement
(以上类似)
(
译者说明:自增和自减操作符的对像一定要是变量,不能是常量.
给几个例子:
#define A 32
new a = 32
如果在程序中用这样的就错了:
A++
--A
//A是一个常量
(a+1)++
//(a+a)也是常量
)
//Note that a++ essentially trims down this code:(本质上和a++是一样的)
a = a + 1
//However, there is another way to write lines of code of this form:
//不管怎样,有其它的方法来实现
a = a OP y
//Where OP is a math operator. It can be shortened to:
//这里的OP是一个数学操作符。可以这样写得更短
a OP= x
//Observe:
a += 1 //This sets a to a + 1 (a=a+1)
a -= b //This sets b to a - b (b=b-1)
a *= 0 //This multiplies a by 0 (a=a*0)
a /= 2 //This divides a by 2. (a=a/2)
However, mathematical operators are not the only operators you are given. There are boolean operators to help you with logical circuits or logical decisions.
不管怎样,数学操作符是不仅仅是你给出的操作符。有一些布尔型的操作符还能够帮助你用合理的思路或合理的逻辑判定
//The and operator takes in the left expression and right expression.
// and 操作符把其左边的和右边的作运算
// If both are "true", then it returns true.
//如果两边为真则返回为真,
//This is false, because 1 returns true and 0 returns false.
//只要有一个值是0则返回的值就是false
//Since both are not true, && returns false.
(1 && 0)
(1 && 2) //Both numbers are "true", therefore the expression is true.
(true && false) //false
(false && false) //false
(true && true) //true
//The other important operator is "or". It returns true if one of two expressions are true.
// 或 操作符(||),只要两个表达式中只要有一个为真就为真
(1 || 0) //true, since one of the values is true.
(1 || 2) //true
(true || true) //true
(false || false) //false
(true || true) //true
There are other operators as well, that you may not use as often. The "bitwise and" operator returns whether a binary bit sequence is contained in another sequence. In the technical terms, it does an "and (&&)" operation on each of the bits in both numbers. For example, say you have the number "9", which is "1001" in binary. If you want to know if that sequence contains the number "8" (1000), you can do:
这有一些其它的操作符,它们可能不常用。位运算中的与("bitwise and")操作符返回一个二进制字节序列是否包含在另一个字节序列中。在专门的术语中,它做一个 &&操作符,在两个数之间进行位运算。例如:如果你用一个数字9,它的二进制代码是1001,如果你想知道这个二进制序列中进否包含8(1000),那么你可以这样做:
//This will return 8, which means 8 is indeed a bit in 9.
//这会返回8,意思是8是的二进制序列在9中
(9 & 8)
//4 (00100) is not a bit inside 16 (10000) and this will return 0.
//4不是一个包含在16中的字节返回0
(16 & 4)
//The next operator is "bitwise or"
//下一个位操作符 | (OR 或)
//which does an "or (||)' operation on each of the bits in both numbers.
//逻辑操作符的一个符号。
//This will take 9 (1001) and match it with 3 (0011), resulting in 1011, or 11.
//9与3进行或位算的结果是11
(9 | 3)
These two operators are also important, but not used often. They are the bitwise shift operators, << is a left shift and >> is a right shift. They shift the bits in a number to one direction.
这还有两个一样很重要的位运算操作符。他们是左移(<<)和右移(<<)。
//This takes the number 3 (00011) and shifts it three places to binary (11000), or 24.
//3的二进制代码(00011)左移3位变成(11000)即十进制24。
(3 << 3)
//This takes the number 24 (11000) and shifts it three places to binary (00011), or 3.
//24的进制进代码(11000)右移三位变成(00011)即十进制3
(24 >> 3)
The last operator is "bitwise not". It returns the exact opposite of whatever is given to it. When used on a number, it will return each of the bits flipped (1 to 0, 0 to 1).
这最后一个位操作符按位取反。
//This returns false SMALL中true的值为1。false值为0)
(!true)
//This returns true
(!false)
//This takes 9 (binary 1001) and makes it 6 (binary 0110).
(!(9))
(
译者加:
关于位运算
位运算有三个:& | ! (与、或、取返)
位与:0 & 0=0 0 & 1=0 1 & 1=1
按位与运算与逻辑与运算相似,按位与运算机没有进位!
如:(8 & 7)
8 1000
& 7 & 0111
0 0000
21 10101
& 8 & 01111
5 00101
运算的规则与逻辑与运算是一样的,还是来一个运用吧!
1:取一个数中的指定位。一个整数a(算是2个字节吧!)想要其中的低8位)怎样做??你一定知了,与一个代8位全是1的进行与运算就OK了。如a为000000111 00001100与00000000 11111111进行位与的话就是:000000000 00001100。
也就是想把那位保留下来就把这个位与1进行与位算。
位或:0 | 0 =0 0 | 1=1 1|1=1
如:
8 1000
| 7 | 0111
15 1111
想对一个数的那个位改为1就与1或
按位取反
!(0)=1 !(1)=0 !(0101100)=1010011
关于左移 << 与右移 >>
SMALL这说的不多,我们也不多说要进一少了解的就找一些书看看.注意的是移动的位后用0补.当然!这不是绝对的!对数值有、无符号数值又不同了,不知SMALL是否有。我不知!
)
Email:jhting515@hotmail.com
|