中国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-08-12 00:50
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » [Help]Code Explanation
Printable Version  3,600 / 16
Floor1 ZJHJ Posted 2010-08-18 00:29
高级用户 Posts 374 Credits 609
I see the following code, very incisive! Please ask the teacher for guidance!
@echo off&setlocal enabledelayedexpansion
:: Code 5
:: Code by CN-DOS terse
set "hx=0123456789ABCDEF"
set/p str=:
for /l %%i in (1 1 8) do (
if !str! GTR 0 (
set/a "n=str&15,str>>=4"
call set h=%%hx:~!n!,1%%!h!
)
)
if not defined h set "h=0"
echo.%str% 的十六进制为:0x!h!
pause

1,
%%i ranges from 1 to 8, why? But no place to see %%i?
2,
set/a "n=str&15,str>>=4"
It means taking the remainder, but I can't understand it at all.....

If written according to my way, it becomes like this:

set /a y= str%%16
set /a ya=str/16%%16
set /a yb=str/256%%16
set /a yc=str/4096%%16
set /a yd=str/65536%%16
set /a ye=str/1048576%%16
set /a yf=str/16777216%%16
set /a yg=str/268435456%%16
set m=0123456789ABCDEF
set cdm=!m:~%yg%,1!!m:~%yf%,1!!m:~%ye%,1!!m:~%yd%,1!!m:~%yc%,1!!m:~%yb%,1!!m:~%ya%,1!!m:~%y%,1!

[ Last edited by ZJHJ on 2010-8-18 at 00:36 ]
Floor2 slore Posted 2010-08-18 13:00
铂金会员 Posts 2,478 Credits 5,212
That loop only controls the number of loop iterations to 8 times. Mainly it's shift operations... For the specific meaning, you can Google relevant bit operations, AND operations, left shifts, right shifts.
Floor3 HAT Posted 2010-08-18 17:40
版主 Posts 5,017 Credits 9,023
Batch processing bit operations and the concepts of original code, one's complement, and two's complement
http://bbs.bathome.net/thread-1844-1-1.html
Floor4 q8249014 Posted 2010-08-20 11:10
初级用户 Posts 45 Credits 175
This code is a section of example demonstration code from the study post ①. It's the one I simplified and corrected.

1. The hexadecimal of 2147483647 is: 0x7FFFFFFF. The limit of set /a [2^31 - 1].
7FFFFFFF is 8 bits. So we can loop at most 8 times.
for is just for looping. %%i is needed by the syntax. So...

2. Reference the post of bit operation
Floor5 doshsyy Posted 2010-08-20 19:00
新手上路 Posts 15 Credits 19
I just control the number of loop iterations to 8 times.
set/a "n=str&15,str>>=4"
n=str&15 is equivalent to n=str%16 in C language. The binary of 15 is 1111. The " & " operation of the binary of str and 1111 only takes the last four bits of the binary of str, that is, the remainder of str/16. For example: 30&16-->00011110&1111=1110=14. That is equivalent to 30%16=14.
str>>4, the binary of str is shifted right by four bits, that is, the last four bits are lost, which is equivalent to str=str/16 and then take the integer, that is, int str=str/16 in C language.
Originally it was written very detailed, but it was said not to post. So the hard - edited content is lost, and I can only be simple. If you want to blame, just blame the forum that you have to register for more than 2 hours to post! (It seems that I have registered for more than 2 hours, and finally I can only post in the command line, speechless!)

[ Last edited by doshsyy on 2010-8-20 at 21:12 ]
Floor6 doshsyy Posted 2010-08-20 19:03
新手上路 Posts 15 Credits 19
I actually find call set h=%%hx:~!n!,1%%!h! more difficult to understand. Why add call, and why is it incorrect without adding call?

[ Last edited by doshsyy on 2010-8-23 at 13:41 ]
Floor7 ZJHJ Posted 2010-08-20 23:39
高级用户 Posts 374 Credits 609
This set/a "n=str&15,str>>=4" is still difficult to understand....
I think from the perspective of learning the principle, writing the code like this is very easy to understand:
@echo off
setlocal enabledelayedexpansion
set /p qw=Enter a decimal number (maximum value 2147483647)
set m=0123456789ABCDEF
set /a y=qw, ys=qw%%16
set h=!m:~%ys%,1!
for /l %%i in (1 1 7) do (
set /a y=!y!/16, ys=!y!/16%%16
call set h=%%m:~!ys!,1%%!h!
)
:ie
if %h:~0,1%==0 (
set h=%h:~1%
goto ie
)
echo The hexadecimal of %qw% is !h!
pause

[ Last edited by ZJHJ on 2010-8-21 at 00:45 ]
Floor8 doshsyy Posted 2010-08-21 07:54
新手上路 Posts 15 Credits 19
Well, it's difficult to understand, but the code is concise, and it uses the basic operators of the system, and it's also fast. This is a common skill of batch processing operation symbols.
Floor9 hsyy Posted 2010-08-21 08:05
新手上路 Posts 2 Credits 6
I want to test the forum's bug. Sure enough, there is one, but it's not serious and doesn't threaten security.

[ Last edited by hsyy on 2010-8-21 at 08:07 ]
Floor10 ZJHJ Posted 2010-08-23 12:39
高级用户 Posts 374 Credits 609
If the efficiency of the program is considered very important, I don't agree that simple code means high efficiency.

I have also done experiments:

Continuously process 10,000 groups of random 9-digit numbers and convert them to hexadecimal.

If using the direct display method, it takes 5 minutes and 20 seconds.

If using for /l %%i in (1 1 8) do (
if!str! GTR 0 (
set/a "n=str&15,str>>=4"
call set h=%%hx:~!n!,1%%!h!
)
)
It takes about 13 minutes and 15 seconds.

If using
:dycx
set /a y=qw, ys=qw%%16
set h=!m:~%ys%,1!
:bb
set /a y=!y!/16, ys=!y!/16%%16
call set h=%%m:~!ys!,1%%!h!
if!y! GTR 0 goto bb

It takes about 22 minutes and 37 seconds.
Floor11 slore Posted 2010-08-23 12:59
铂金会员 Posts 2,478 Credits 5,212
This is not because the code is simple... Bitwise operations happen to be simple...

Theoretically, bitwise operations are fast, so the code uses bit operations.

It is to use bit operations for efficiency, not because bit operations make the code concise...

The interpretation execution of bat is not something we can control. If it were a high-level language, bit operations would definitely be fast.
Here it also involves strings...

What does the 10th floor directly display mean? Directly echo without conversion?

Looking at your data, it seems that set/a "n=str&15,str>>=4" is fast for 8, 9 minutes?
But it doesn't match what you said
If you attach great importance to the efficiency of the program, I don't agree that simple code means high efficiency.
Floor12 doshsyy Posted 2010-08-23 13:53
新手上路 Posts 15 Credits 19
I didn't say that concise code will have high execution efficiency, I'm not that ignorant. When it comes to code quality, we need to consider time complexity and storage space requirements, which is not necessary for small programs. It's definitely necessary for large programs. For the above program, it just happens to use logical operations and shift operations with efficient binary bits and is concise. So I said "But the code is concise, and it calls the system's basic operators, and it's fast." I don't know why you said I said that concise code has high execution efficiency! Also, I'm confused about the result that you got that the "direct display method" is more efficient than direct binary execution!
Floor13 doshsyy Posted 2010-08-23 15:18
新手上路 Posts 15 Credits 19
@echo off&setlocal enabledelayedexpansion
set t1=!time!
echo %t1%
for /L %%i in (1 1 10000) do (
set /A str=!random!
set s=!str!
set hx=0123456789ABCDEF
for /L %%j in (1 1 8) do (
if !str! GTR 0 (
set /A "n=!str!&15,str>>=4"
call set h=%%hx:~!n!,1%%!h!
)
)
if not defined h set "h=0"
echo.!s! 的十六进制为:0x!h!
set h=
)
set t2=!time!
echo %t2%
for /F "tokens=2,3delims=:" %%i in ("%t1%") do (
set t1m=%%i
set t1s=%%j
set t1s=!t1s:~0,-3!
)
echo %t1m% !t1s!
for /F "tokens=2,3delims=:" %%i in ("%t2%") do (
set t2m=%%i
set t2s=%%j
set t2s=!t2s:~0,-3!
)
echo %t2m% !t2s!
set /A "tm=%t2m%-%t1m%,ts=!t2s!-!t1s!"
echo %tm%:%ts%
pause
Echo 1:2 (1 minute 02 seconds)
@echo off&setlocal enabledelayedexpansion
set t1=!time!
for /L %%i in (1 1 10000) do (
set /A str=!random!
set s=!str!
set hx=0123456789ABCDEF
for /L %%j in (1 1 8) do (
if !str! GTR 0 (
set /A "n=!str!&15,str>>=4"
call set h=%%hx:~!n!,1%%!h!
)
)
if not defined h set "h=0"
echo !s!的十六进制为:0x!h!
set h=
)
set t2=!time!
for /F "tokens=2,3delims=:" %%i in ("%t1%") do (
set t1m=%%i
set t1s=%%j
set t1s=!t1s:~0,-3!
)
for /F "tokens=2,3delims=:" %%i in ("%t2%") do (
set t2m=%%i
set t2s=%%j
set t2s=!t2s:~0,-3!
)
set /A "tm=%t2m%-%t1m%,ts=!t2s!-!t1s!"
echo %tm%:%ts%
pause
Echo 1:1 (1 minute 01 second)
I don't know how you did it on the 10th floor. The running time is so long and the gap is so big. I guess there's a mistake in the code!
Floor14 ZJHJ Posted 2010-08-23 19:04
高级用户 Posts 374 Credits 609
What I mean is:
If using for /l %%i in (1 1 8) do (
if !str! GTR 0 (
set/a "n=str&15,str>>=4"
call set h=%%hx:~!n!,1%%!h!
)
)
Or using for /l %%i in (1 1 7) do (
set /a y=!y!/16, ys=!y!/16%%16
call set h=%%m:~!ys!,1%%!h!
)
In this way, that is, the for loop code is very simple, but the processing speed is not fast. It takes about 13 minutes and 15 seconds.
If using
:bb
set /a y=!y!/16, ys=!y!/16%%16
call set h=%%m:~!ys!,1%%!h!
if !y! GTR 0 goto bb
In this way of processing, the code is also very simple, but the processing speed is the slowest. It takes about 22 minutes and 37 seconds.
If using

set /a y= qw%%16
set /a y1=qw/16%%16
set /a y2=qw/256%%16
set /a y3=qw/4096%%16
set /a y4=qw/65536%%16
set /a y5=qw/1048576%%16
set /a y6=qw/16777216%%16
set /a y7=qw/268435456%%16
set h=!m:~%y7%,1!!m:~%y6%,1!!m:~%y5%,1!!m:~%y4%,1!!m:~%y3%,1!!m:~%y2%,1!!m:~%y1%,1!!m:~%y%,1!
In this way is the fastest, taking 5 minutes and 20 seconds.
The problem lies in for or goto

[ Last edited by ZJHJ on 2010-8-23 at 19:08 ]

Attachments
代码比较.rar (44.8 KiB)
Floor15 HAT Posted 2010-08-24 11:18
版主 Posts 5,017 Credits 9,023
Basic knowledge of variable delayed expansion and variable nesting
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023