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-07-31 21:17
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to use bitwise operations in set/a? View 7,267 Replies 42
Original Poster Posted 2006-10-18 03:19 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Originally posted by pengfei at 2006-10-17 23:11:
Interesting, the first post by moderator qwe1234567 forgot the difference between "%" and "%%" in cmd and batch processing when taking the remainder.

The input line is too long, this may be because the system has insufficient memory allocated for environment variables..

Can this be blamed on me? It's also because Microsoft didn't explain the usage of SET/A clearly. Just SET /A, I believe everyone doesn't know how to use all the evaluated numeric expressions. How many of the following do you know?
() - Grouping
* / % - Arithmetic operators
+ - - Arithmetic operators
<< >> - Logical shift
- Bitwise "AND"
^ - Bitwise "XOR"
| - Bitwise "OR"
= *= /= %= += -= - Assignment
&= ^= |= <<= >>=
, - Expression separator
I think except for * / %+ - = *= /= %= += -= - which can be used, the others are not used. Hehe!
Floor 2 Posted 2006-10-18 04:31 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
The rest are bitwise operations. Those who have studied C all know that

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 3 Posted 2006-10-18 19:54 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Originally posted by electronixtar at 2006-10-18 04:31:
The rest are bitwise operations. Anyone who has studied C knows

How to use it then?
Floor 4 Posted 2006-10-18 21:59 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
Tutorials in C language. If you still don't understand, I can consider writing a bat example. Bitwise operations are relatively fundamental operations in computers, characterized by high efficiency, but the disadvantage is that they are not easy to use, heh heh



Bitwise Operations

  The various operations introduced earlier are all performed at the byte as the most basic bit. But in many system programs, operations or processing at the bit (bit) level are often required. The C language provides bitwise operation functions, which enables the C language to be used to write system programs like assembly language.

  1. Bitwise Operators The C language provides six bitwise operators:

  & Bitwise AND
  | Bitwise OR
  ^ Bitwise XOR
  ~ Ones' complement
  << Left shift
  >> Right shift

  1. Bitwise AND Operation The bitwise AND operator "&" is a binary operator. Its function is that the corresponding binary bits of the two numbers involved in the operation are ANDed. Only when the corresponding two binary bits are both 1, the result bit is 1; otherwise, it is 0. The numbers involved in the operation appear in two's complement form.

  For example: 9&5 can be written as the following formula: 00001001 (binary two's complement of 9) & 00000101 (binary two's complement of 5) 00000001 (binary two's complement of 1) It can be seen that 9&5=1.

  Bitwise AND operation is usually used to clear certain bits to 0 or retain certain bits. For example, to clear the high eight bits of a to 0 and retain the low eight bits, you can perform a&255 operation (the binary number of 255 is 0000000011111111).

main(){
 int a=9,b=5,c;
 c=a&b;
 printf("a=%d\nb=%d\nc=%d\n",a,b,c);
}

  2. Bitwise OR Operation The bitwise OR operator "|" is a binary operator. Its function is that the corresponding binary bits of the two numbers involved in the operation are ORed. As long as one of the corresponding two binary bits is 1, the result bit is 1. The two numbers involved in the operation both appear in two's complement form.

   For example: 9|5 can be written as the following formula:

00001001|00000101
00001101 (decimal is 13) It can be seen that 9|5=13
main(){
 int a=9,b=5,c;
 c=a|b;
 printf("a=%d\nb=%d\nc=%d\n",a,b,c);
}

  3. Bitwise XOR Operation The bitwise XOR operator "^" is a binary operator. Its function is that the corresponding binary bits of the two numbers involved in the operation are XORed. When the corresponding two binary bits are different, the result is 1. The numbers involved in the operation still appear in two's complement form. For example, 9^5 can be written as the following formula:

00001001^00000101 00001100 (decimal is 12)
main(){
 int a=9;
 a=a^15;
 printf("a=%d\n",a);
}

  4. Ones' Complement Operation The ones' complement operator ~ is a unary operator with right associativity. Its function is to invert the binary bits of the number involved in the operation. For example, the operation of ~9 is: ~(0000000000001001) The result is: 1111111111110110

  5. Left Shift Operation The left shift operator "<<" is a binary operator. Its function is to shift all the binary bits of the operand on the left of "<<" to the left by several bits, and the number on the right of "<<" specifies the number of bits to move.

  High bits are discarded, and low bits are filled with 0. For example: a<<4 means shifting all the binary bits of a to the left by 4 bits. If a=00000011 (decimal 3), after shifting left by 4 bits, it is 00110000 (decimal 48). 6. Right Shift Operation The right shift operator ">>" is a binary operator. Its function is to shift all the binary bits of the operand on the left of ">>" to the right by several bits, and the number on the right of ">>" specifies the number of bits to move.

  For example: Let a=15, a>>2 means shifting 000001111 to the right to get 00000011 (decimal 3). It should be noted that for signed numbers, the sign bit will move along when shifting to the right. When it is a positive number, the highest bit is filled with 0, and when it is a negative number, the sign bit is 1. Whether the highest bit is filled with 0 or 1 depends on the regulations of the compilation system. Turbo C and many systems specify that it is filled with 1.

main(){
 unsigned a,b;
 printf("input a number: ");
 scanf("%d",&a);
 b=a>>5;
 b=b&15;
 printf("a=%d\tb=%d\n",a,b);
}

  Please take another example!

main(){
 char a='a',b='b';
 int p,c,d;
 p=a;
 p=(p<<8)|b;
 d=p&0xff;
 c=(p&0xff00)>>8;
 printf("a=%d\nb=%d\nc=%d\nd=%d\n",a,b,c,d);
}


[ Last edited by electronixtar on 2006-10-18 at 22:06 ]

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 5 Posted 2006-10-18 22:01 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Floor 6 Posted 2006-10-18 22:24 ·  中国 北京 东城区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
electronixtar is too lazy to write, haha...
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 7 Posted 2006-10-18 22:30 ·  中国 北京 东城区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Many tutorials (most tutorials) write about bit operations (almost) like textbook-style indoctrination, and there's no exciting place to be found : )

Just like when learning mathematics during student days, except for geometry where you can know what this thing is used for, things like a bunch of micro-calculus and so on that make you want to commit suicide. The book doesn't say the practicality of these things. But when it comes to financial analysis applications, these are all necessary. By then, I've long forgotten the formulas : )

Many course educations are too rigid ~ : )
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 8 Posted 2006-10-18 22:30 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
……Personally, there's not much difference between the operations in cmd and in C.


electronixtar is too lazy to write, haha……


…………………………Don't force me to write…………………………-_-!

[ Last edited by electronixtar on 2006-10-18 at 22:35 ]

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 9 Posted 2006-10-18 22:36 ·  中国 北京 东城区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Haha... Agreed~ :)
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 10 Posted 2006-10-18 22:38 ·  中国 江苏 苏州 电信
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
There are a few that really don't know how to use. I'll write a few when I have time. = =b
Floor 11 Posted 2006-10-18 22:43 ·  中国 北京 东城区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
It is suggested that brother electronixtar start a new special thread to discuss batch processing bitwise operations right now : )
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 12 Posted 2006-10-18 22:48 ·  中国 北京 东城区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Everyone knows how to use these symbols, and books explain them clearly : )

However, I really dislike the teaching methods in books or textbooks,

It can't make people immediately have that fascinated, forget to eat and sleep, crazy and must try now idea when seeing the bitwise operation course...

So, this is the failure of the teaching method!

It should be like this, after reading and practicing...

  "Oh, so that's how it is!"
  "Oh my god! Before I saw this tutorial, I actually took 10 years of detours!!!"
  "Oh! Bitwise operations are so amazing, they can also be used for intelligent operations in games like five-in-a-row..."
  
  Wait...

  What I dislike most about textbooks is that a large number of operations just don't explain the application inspirations of these calculations in real life and programming...
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 13 Posted 2006-10-18 22:52 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Originally posted by electronixtar at 2006-10-18 22:30:
……Personally, I think there is no difference between the operations in cmd and in C.
…………………………Don't force me to write…………………………-_-!

Then you write a pure bit operation in CMD for us to see! I really want to force you to write, hehe, I believe everyone wants to see the bit operation in CMD.
Floor 14 Posted 2006-10-18 22:53 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
This……You guys are enough~~

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 15 Posted 2006-10-18 22:55 ·  中国 江苏 苏州 电信
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
Go ahead, we're looking forward to it.

PS: By the way, why do I often see other posts when I refresh, and then it comes back to this post after a while? It's strange, and I've encountered this more than once.
Forum Jump: