中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-15 12:37
47,812 topics / 349,917 posts / today 0 new / 48,268 members
DOS批处理 & 脚本技术(批处理室) » How to use bitwise operations in set/a?
Printable Version  7,827 / 42
Floor1 不得不爱 Posted 2006-10-18 03:19
超级版主 Posts 2,044 Credits 5,310 From 四川南充
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!
Floor2 electronixtar Posted 2006-10-18 04:31
铂金会员 Posts 2,672 Credits 7,493
The rest are bitwise operations. Those who have studied C all know that
Floor3 不得不爱 Posted 2006-10-18 19:54
超级版主 Posts 2,044 Credits 5,310 From 四川南充
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?
Floor4 electronixtar Posted 2006-10-18 21:59
铂金会员 Posts 2,672 Credits 7,493
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 ]
Floor5 不得不爱 Posted 2006-10-18 22:01
超级版主 Posts 2,044 Credits 5,310 From 四川南充
But you're using C language. I need something that works in CMD.
Floor6 redtek Posted 2006-10-18 22:24
金牌会员 Posts 1,147 Credits 2,902
electronixtar is too lazy to write, haha...
Floor7 redtek Posted 2006-10-18 22:30
金牌会员 Posts 1,147 Credits 2,902
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 ~ : )
Floor8 electronixtar Posted 2006-10-18 22:30
铂金会员 Posts 2,672 Credits 7,493
……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 ]
Floor9 redtek Posted 2006-10-18 22:36
金牌会员 Posts 1,147 Credits 2,902
Haha... Agreed~ :)
Floor10 NaturalJ0 Posted 2006-10-18 22:38
银牌会员 Posts 533 Credits 1,181
There are a few that really don't know how to use. I'll write a few when I have time. = =b
Floor11 redtek Posted 2006-10-18 22:43
金牌会员 Posts 1,147 Credits 2,902
It is suggested that brother electronixtar start a new special thread to discuss batch processing bitwise operations right now : )
Floor12 redtek Posted 2006-10-18 22:48
金牌会员 Posts 1,147 Credits 2,902
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...
Floor13 不得不爱 Posted 2006-10-18 22:52
超级版主 Posts 2,044 Credits 5,310 From 四川南充
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.
Floor14 electronixtar Posted 2006-10-18 22:53
铂金会员 Posts 2,672 Credits 7,493
This……You guys are enough~~
Floor15 NaturalJ0 Posted 2006-10-18 22:55
银牌会员 Posts 533 Credits 1,181
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.
1 2 3  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023