China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-07 15:17
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Arithmetic operators and logical operators in the batch processing set command, for beginners to see View 2,014 Replies 10
Original Poster Posted 2008-02-11 15:22 ·  中国 吉林 吉林市 联通
中级用户
★★
Credits 307
Posts 130
Joined 2008-02-01 21:29
18-year member
UID 109981
Gender Male
Status Offline
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
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
everest79 +11 2008-02-11 16:36
Floor 2 Posted 2008-02-11 16:40 ·  中国 广东 广州 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
Some commands are binary. The original poster might be better to put binary examples on it.
Floor 3 Posted 2008-02-11 16:46 ·  中国 吉林 吉林市 联通
中级用户
★★
Credits 307
Posts 130
Joined 2008-02-01 21:29
18-year member
UID 109981
Gender Male
Status Offline
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
Floor 4 Posted 2008-02-11 17:01 ·  中国 广东 广州 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
Floor 5 Posted 2008-02-11 17:48 ·  中国 吉林 吉林市 联通
中级用户
★★
Credits 307
Posts 130
Joined 2008-02-01 21:29
18-year member
UID 109981
Gender Male
Status Offline
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
Floor 6 Posted 2008-02-11 18:17 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Maybe first calculate 3*2^2+3

11---multiply by 2^2--->1100

1100+11=1111 (15)
Floor 7 Posted 2008-02-11 18:56 ·  中国 吉林 吉林市 联通
中级用户
★★
Credits 307
Posts 130
Joined 2008-02-01 21:29
18-year member
UID 109981
Gender Male
Status Offline
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.
Floor 8 Posted 2008-02-11 19:18 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
……
Binary is not widely used in BAT, but it's definitely good to understand.
Floor 9 Posted 2008-02-12 15:55 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
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
Floor 10 Posted 2008-02-12 18:38 ·  中国 吉林 吉林市 联通
中级用户
★★
Credits 307
Posts 130
Joined 2008-02-01 21:29
18-year member
UID 109981
Gender Male
Status Offline
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.
Floor 11 Posted 2008-02-12 18:40 ·  中国 吉林 吉林市 联通
中级用户
★★
Credits 307
Posts 130
Joined 2008-02-01 21:29
18-year member
UID 109981
Gender Male
Status Offline
What the upstairs said is about the usage of ~, I forgot to mention it
Forum Jump: