|
pinghu
中级用户
  
积分 307
发帖 130
注册 2008-2-1
状态 离线
|
   『楼 主』:
批处理set命令中的算数运算符和逻辑运算符,新手请看
使用 LLM 解释/回答一下
批处理set命令中的算数运算符和逻辑运算符,新手请看
Dos下的写法 批处理中的写法 符号的含义和取值范围
按位异或:num1^^num2 num1^^num2 转2进制补码,按位判断,相异为1,相同为0,转10进制输出,
按位与: num1^&num2 num1^&num2 转2进制补码,按位判断,全1为1,否则为0,转10进制输出
按位或: num1^|num2 num1^|num2 转2进制补码,按位判断,全0为0,否则为1,转10进制输出
加: num1+num2 num1+num2
减: num1-num2 num1-num2
乘: num1*num2 num1*num2
除: num1/num2 num1/num2 结果向零取整,num2不为0
求余: num1%num2 num1%%num2 num2不为0,num2为负数当同绝对值的正数处理,num2为负数时当正数计算,然后结果取负
按位取反:~num ~num 转2进制,1变成0,0变成1,以补码的十进制形式输出
逻辑非: !num !num num为0输出1,不为0输出0
求负: -num -num
逻辑左移:num1^<^<num2 num1^<^<num2 转2进制补码,num1所有位向左移num2个位数,移几位后面填几个0,转十进制输出,num2为负时输出0
逻辑右移:num1^>^>num2 num1^>^>num2 转2进制补码,num1所有为向右移num2个位数,转十进制输出,num2为负时输出0
Arithmetic operators and logical operators in the batch processing set command, for beginners to see
DOS writing Batch processing writing Meaning of the symbol and range of values
Bitwise XOR: num1^^num2 num1^^num2 Convert to 2's complement binary, judge bit by bit, different is 1, same is 0, convert to decimal output,
Bitwise AND: num1^&num2 num1^&num2 Convert to 2's complement binary, judge bit by bit, all 1s is 1, otherwise 0, convert to decimal output
Bitwise OR: num1^|num2 num1^|num2 Convert to 2's complement binary, judge bit by bit, all 0s is 0, otherwise 1, convert to decimal output
Addition: num1+num2 num1+num2
Subtraction: num1-num2 num1-num2
Multiplication:num1*num2 num1*num2
Division: num1/num2 num1/num2 The result is truncated towards zero, num2 is not 0
Remainder: num1%num2 num1%%num2 num2 is not 0, when num2 is negative, treat it as the same absolute value positive number, when num2 is negative, calculate as positive, then take the negative of the result
Bitwise NOT: ~num ~num Convert to binary, 1 becomes 0, 0 becomes 1, output in decimal form of 2's complement
Logical NOT: !num !num Output 1 if num is 0, output 0 if not 0
Negation: -num -num
Logical left shift: num1^<^<num2 num1^<^<num2 Convert to 2's complement binary, all bits of num1 are shifted left by num2 digits, fill several 0s at the back, convert to decimal output, output 0 if num2 is negative
Logical right shift: num1^>^>num2 num1^>^>num2 Convert to 2's complement binary, all bits of num1 are shifted right by num2 digits, convert to decimal output, output 0 if num2 is negative
|
|
2008-2-11 15:22 |
|
|
everest79
金牌会员
      一叶枝头,万树皆春
积分 2564
发帖 1127
注册 2006-12-25
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
一些命令是二进制的,楼主套上二进制例子可能会更好点
Some commands are binary. The original poster might be better to put binary examples on it.
|
|
2008-2-11 16:40 |
|
|
pinghu
中级用户
  
积分 307
发帖 130
注册 2008-2-1
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
好建议,需要把二进制基础知识的讲解加上,要不然新手(我也是了……)不知道什么叫补码,什么叫进制,我需要筹备这方面的教程了
Good suggestion, need to add the explanation of binary basic knowledge, otherwise novices (I am also...) don't know what two's complement is, what base is, I need to prepare this aspect of the tutorial
|
|
2008-2-11 16:46 |
|
|
everest79
金牌会员
      一叶枝头,万树皆春
积分 2564
发帖 1127
注册 2006-12-25
状态 离线
|
|
2008-2-11 17:01 |
|
|
pinghu
中级用户
  
积分 307
发帖 130
注册 2008-2-1
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
什么叫进制
当我们用数字表示数量时,存在一个问题:
我们要表示的事物的量可能很大,数字的数量是有限的,如何用有限的数字表示
很多的数量?
有效的办法就是把数字连起来,利用数字位置的不同来表示他所代表的数量
,如123中的1代表100,2代表20,1代表1
初步理解,进制就是我们表是一个完整的正整数集需要的字符的数量
十进制:我们用的最多,他用了123456790十个数字
二进制:用了1 和0两个数字,计算机使用二进制是依赖于电路的基本结构,可以说是“被迫”使用(有关资料说,3进制是“最优秀的”,可惜在大型集成电路上很难实现)要了解计算机,第一步就是了解2进制
下面列举几个2进制数
111010001
110001100
看到了吧?只有1和0(别问我为什么2进制没有2?,我晕!)
加发运算和十进制类似,上过小学的都会,无非是满2进1!
算乘法就是算加法,3乘5,计算机就计算3+3+3+3+3,计算机是不是很笨?只认识0和1,算减法也要当加法算,减法怎么算的我以后在说
What is the number system
When we use numbers to represent quantities, there is a problem:
The quantity of the thing we want to represent may be very large, and the number of digits is limited. How to use a limited number of digits to represent
a lot of quantities?
An effective way is to connect the digits and use the different positions of the digits to represent the quantity it represents
, such as 123, where 1 represents 100, 2 represents 20, and 1 represents 1
Preliminary understanding, the number system is the number of characters we need to represent a complete set of positive integers
Decimal: We use it the most. It uses ten digits: 123456790
Binary: It uses two digits, 1 and 0. Computers use binary because of the basic structure of the circuit. It can be said to be "forced" to use (relevant materials say that base 3 is "the best", but it is very difficult to implement on large integrated circuits) To understand computers, the first step is to understand binary
The following are several binary numbers
111010001
110001100
You see? Only 1 and 0 (don't ask me why there is no 2 in binary?, I'm confused!)
Addition operation is similar to decimal. Those who have been to primary school will all, it's nothing more than carrying 1 when full of 2!
Multiplication is to calculate addition. 3 times 5, the computer calculates 3+3+3+3+3. Is the computer very stupid? It only recognizes 0 and 1. Subtraction also needs to be calculated as addition. I'll talk about how subtraction is calculated later
|
|
2008-2-11 17:48 |
|
|
slore
铂金会员
      
积分 5212
发帖 2478
注册 2007-2-8
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
可能会先算 3*2^2+3
11---乘以2^2--->1100
1100+11=1111 (15)
Maybe first calculate 3*2^2+3
11---multiply by 2^2--->1100
1100+11=1111 (15)
|
|
2008-2-11 18:17 |
|
|
pinghu
中级用户
  
积分 307
发帖 130
注册 2008-2-1
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
什么叫补码
负数的表示法
负数要表示成2进制怎么半呢?你会说,那就在前面加个负号。问题是计算机只认
识1和0啊,负号怎么表示呢?于是有人提出在2进制数字的前面加上1代表负数
加上0代表正数。这样表示很直观,但是有个问题,0怎么表示?存在两个零,正
的零等于负的零,就因为这两个零给数据运算带来了很大的麻烦,我们才引进了
“补码”的概念。“补码”里只有一个零,每个单一数值和唯一编码相对应,才
符合计算机运算的要求。
正数的补码和原码一致,0的补码是0000000,负数的补码是他相反数逐位取反再
加一
比如1的源码表示为00000001,逐位取反后变成了11111110,(称为反码),在加
1 就变成了11111111,这个11111111就表示负1
还有一种方法理解11111111表示-1:0不是表示为0000000吗,那0减去1 就表示-1
了,什么?不够减?向想象中的第九位借位(暂时不要考虑高位的问题),结果
出来了11111111,那负2呢?-1在减1被,你看:
11111111在减1等于11111110,其他的负数聪明的读者,你会表示了吗?
在这里要注意一个特殊的数:-128,表示成10000000
-129呢?很抱歉在八个位数的二进制中无法表式,需要用十六位二进制数表式如
下1111111101111111这个数再加1就是1111111110000000也就是-128
有人要问了,10000000和1111111110000000不同啊,怎么表是同样的数?
那我们用正数的时候00000001和1都表示1,1前面的全零串没有意义
负数正好相反,零前面的全1串没有意义,相加多少都行
我举例子的时候用的都是8位的和16位的,8位组成一个字节,表式一个数最少用8
位,数字再大就用16位,再就是18位的。反正是8的倍数。
在数字前面加上若干零或者1的过程,我们叫它“扩展到高位字节”
上个帖子提到了怎么算减法,就是把减正看成加负,负数一律表示成补码
,补码不分正负,只要相加就行了,简单吧?
这一讲最后说说取值范围:一个8位能表示2的8次方个数,就是256
一个16位能表示65536个数
我们不需要表示负数的时候使用“无符号数”8位的取值范围是0~255,16位的取
值范围是0~65535.无符号数就是不用补码的数
我们需要表示负数的时候要用“有符号数”8位的取值范围是-128~127,16位的取
值范围是-32768~32767
下一讲要讲逻辑运算符和算术运算符
What is two's complement?
Representation of negative numbers
How to represent a negative number in binary? You might say, then put a negative sign in front. The problem is that computers only recognize 1 and 0. How to represent the negative sign? So someone proposed that adding 1 in front of the binary number represents a negative number and adding 0 represents a positive number. This representation is intuitive, but there is a problem: how to represent 0? There are two zeros, positive zero equals negative zero. It is because of these two zeros that it brings great trouble to data operations. That's why we introduced the concept of "two's complement". In two's complement, there is only one zero, and each single value corresponds to a unique code, which meets the requirements of computer operations.
The two's complement of a positive number is the same as its original code. The two's complement of 0 is 0000000. The two's complement of a negative number is obtained by inverting each bit of its opposite number and then adding 1.
For example, the original code of 1 is represented as 00000001. Inverting each bit gives 11111110 (called the one's complement), and then adding 1 gives 11111111, which represents -1.
There is another way to understand that 11111111 represents -1: 0 is represented as 0000000, then 0 minus 1 represents -1. What? Not enough to subtract? Borrow from the imaginary ninth bit (temporarily don't consider the high bit issue), and the result comes out as 11111111. Then what about -2? Subtract 1 from -1, you see: 11111111 minus 1 is 11111110. Readers who are smart, can you represent other negative numbers?
Here, we need to pay attention to a special number: -128, which is represented as 10000000.
What about -129? Unfortunately, it cannot be represented in an 8-bit binary number. It needs to be represented in a 16-bit binary number as follows: 1111111101111111. Adding 1 to this number gives 1111111110000000, which is -128.
Some people might ask: 10000000 and 1111111110000000 are different, how can they represent the same number?
When using positive numbers, 00000001 and 1 both represent 1, and the leading all-zero string of 1 has no meaning.
For negative numbers, it's the opposite. The leading all-1 string of 0 has no meaning, and you can add any number.
The examples I gave are all 8-bit and 16-bit. 8 bits form a byte. At least 8 bits are used to represent a number. If the number is larger, 16 bits are used, and then 18 bits, etc. Anyway, it's a multiple of 8.
The process of adding several zeros or 1s in front of a number is called "extending to the high-order byte".
The previous post mentioned how to calculate subtraction, which is to treat subtracting a positive number as adding a negative number. Negative numbers are all represented as two's complement. Two's complement has no distinction between positive and negative, just add them. Simple, right?
At the end of this lecture, let's talk about the range of values: an 8-bit can represent 2 to the power of 8 numbers, which is 256. A 16-bit can represent 65536 numbers.
When we don't need to represent negative numbers, we use "unsigned numbers". The range of values for an 8-bit unsigned number is 0~255, and for a 16-bit unsigned number is 0~65535. Unsigned numbers are numbers that don't use two's complement.
When we need to represent negative numbers, we use "signed numbers". The range of values for an 8-bit signed number is -128~127, and for a 16-bit signed number is -32768~32767.
The next lecture will talk about logical operators and arithmetic operators.
|
|
2008-2-11 18:56 |
|
|
slore
铂金会员
      
积分 5212
发帖 2478
注册 2007-2-8
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
……
2进制在BAT里应用不多哦~
不过了解当然好。
……
Binary is not widely used in BAT, but it's definitely good to understand.
|
|
2008-2-11 19:18 |
|
|
everest79
金牌会员
      一叶枝头,万树皆春
积分 2564
发帖 1127
注册 2006-12-25
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
Originally posted by pinghu at 2008-2-11 03:22 PM:
批处理set命令中的算数运算符和逻辑运算符,新手请看
Dos下的写法 批处理中的写法 符号的含义和取值范围
按位异或:num1^^num2 num1^^num2 转2进制补码,按位判断,相异为1,相同为0,转10进制输出,
按位与: num1^&num2 num1^&num2 转2进制补码,按位判断,全1为1,否则为0,转10进制输出
按位或: num1^|num2 num1^|num2 转2进制补码,按位判断,全0为0,否则为1,转10进制输出
加: num1+num2 num1+num2
减: num1-num2 num1-num2
乘: num1*num2 num1*num2
除: num1/num2 num1/num2 结果向零取整,num2不为0
求余: num1%num2 num1%%num2 num2不为0,num2为负数当同绝对值的正数处理,num2为负数时当正数计算,然后结果取负
按位取反:~num ~num 转2进制,1变成0,0变成1,以补码的十进制形式输出
逻辑非: !num !num num为0输出1,不为0输出0
求负: -num -num
逻辑左移:num1^<^<num2 num1^<^<num2 转2进制补码,num1所有位向左移num2个位数,移几位后面填几个0,转十进制输出,num2为负时输出0
逻辑右移:num1^>^>num2 num1^>^>num2 转2进制补码,num1所有为向右移num2个位数,转十进制输出,num2为负时输出0
^运算符
转2进制补码,按位判断,相异为1,相同为0,转10进制输出
10 ^ 8 = 2
00001010 ^ 00001000 = 00000010
&运算符
转2进制补码,按位判断,全1为1,否则为0,转10进制输出
10 & 8 = 8
00001010 & 00001000 = 00001000
|运算符
转2进制补码,按位判断,全0为0,否则为1,转10进制输出
10 | 8 = 10
00001010 | 00001000 = 00001010
>>运算符
转2进制补码,num1所有位向左移num2个位数,移几位后面填几个0,转十进制输出,num2为负时输出0
10 >>1 = 5
00001010 >>1 = 00000101
<<运算符
转2进制补码,num1所有为向右移num2个位数,转十进制输出,num2为负时输出0
10 <<1 = 20
00001010 >>1 = 000010100
~比较难理解,那位达人举个直观点的例子来,楼上的没看明白
Originally posted by pinghu at 2008-2-11 03:22 PM:
Arithmetic and logical operators in the batch processing set command, for newbies to see
Dos writing method Batch processing writing method Meaning of the symbol and value range
XOR bitwise: num1^^num2 num1^^num2 Convert to 2's complement in binary, judge bit by bit, different is 1, same is 0, convert to decimal output,
Bitwise AND: num1^&num2 num1^&num2 Convert to 2's complement in binary, judge bit by bit, all 1 is 1, otherwise 0, convert to decimal output
Bitwise OR: num1^|num2 num1^|num2 Convert to 2's complement in binary, judge bit by bit, all 0 is 0, otherwise 1, convert to decimal output
Add: num1+num2 num1+num2
Subtract: num1-num2 num1-num2
Multiply: num1*num2 num1*num2
Divide: num1/num2 num1/num2 The result is truncated towards zero, num2 is not 0
Remainder: num1%num2 num1%%num2 num2 is not 0, when num2 is negative, treat it as the absolute value positive, when num2 is negative, calculate as positive, then take the result negative
Bitwise NOT: ~num ~num Convert to binary, 1 becomes 0, 0 becomes 1, output in decimal form of 2's complement
Logical NOT: !num !num If num is 0 output 1, otherwise output 0
Negate: -num -num
Logical left shift: num1^<^<num2 num1^<^<num2 Convert to 2's complement in binary, all bits of num1 are shifted left by num2 digits, fill several 0s at the back after shifting several digits, convert to decimal output, output 0 when num2 is negative
Logical right shift: num1^>^>num2 num1^>^>num2 Convert to 2's complement in binary, all bits of num1 are shifted right by num2 digits, convert to decimal output, output 0 when num2 is negative
^ operator
Convert to 2's complement in binary, judge bit by bit, different is 1, same is 0, convert to decimal output
10 ^ 8 = 2
00001010 ^ 00001000 = 00000010
& operator
Convert to 2's complement in binary, judge bit by bit, all 1 is 1, otherwise 0, convert to decimal output
10 & 8 = 8
00001010 & 00001000 = 00001000
| operator
Convert to 2's complement in binary, judge bit by bit, all 0 is 0, otherwise 1, convert to decimal output
10 | 8 = 10
00001010 | 00001000 = 00001010
>> operator
Convert to 2's complement in binary, all bits of num1 are shifted left by num2 digits, fill several 0s at the back after shifting several digits, convert to decimal output, output 0 when num2 is negative
10 >>1 = 5
00001010 >>1 = 00000101
<< operator
Convert to 2's complement in binary, all bits of num1 are shifted right by num2 digits, convert to decimal output, output 0 when num2 is negative
10 <<1 = 20
00001010 >>1 = 000010100
~ is more difficult to understand, which expert can give a more intuitive example, the upstairs didn't understand
|
|
2008-2-12 15:55 |
|
|
pinghu
中级用户
  
积分 307
发帖 130
注册 2008-2-1
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
13的二进制是1101,前面补上0000,变成00001101,在按位取反,变成11110010,这是个补码,转换成十进制,变成-14
另外楼上的部分运算符写法有误,不要把^^写成^,其他的符号的正确写法参照一楼的,if /?中的帮助中有错误,
下面讲讲负数的补码怎么转化成数值,以11110010为例,首先按位取反变成00001101,在加一,变成00001110,转化成二进制的14,在加-,变成-14
负数的补码转换成数值的规律是取反加1,再加负号
例子不错,谢谢你的补充,就是运算符写错了
有什么我没讲清楚的请回帖
The binary of 13 is 1101. Add 0000 in front to become 00001101. Then invert the bits to become 11110010. This is a two's complement. Convert it to decimal to become -14.
In addition, the operator writing of some people upstairs is wrong. Don't write ^^ as ^. The correct writing of other symbols refers to the first floor. There are errors in the help in if /?.
Now let's talk about how to convert the two's complement of a negative number into a value. Take 11110010 as an example. First invert the bits to become 00001101. Then add 1 to become 00001110. Convert it to binary 14. Then add a minus sign to become -14.
The rule for converting the two's complement of a negative number into a value is to invert the bits, add 1, and then add a minus sign.
The example is good. Thank you for the supplement. It's just that the operator was written wrong.
If there is anything I didn't make clear, please reply.
|
|
2008-2-12 18:38 |
|
|
pinghu
中级用户
  
积分 307
发帖 130
注册 2008-2-1
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
楼上讲的是~的用法啊,忘说了
What the upstairs said is about the usage of ~, I forgot to mention it
|
|
2008-2-12 18:40 |
|
|