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-12 03:34
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help]Code Explanation View 3,607 Replies 16
Original Poster Posted 2010-08-18 00:29 ·  中国 四川 南充 联通
高级用户
★★★
Credits 609
Posts 374
Joined 2006-08-02 22:38
20-year member
UID 59720
Status Offline
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 ]
Floor 2 Posted 2010-08-18 13:00 ·  中国 北京 263网络通信电信数据中心
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
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.
S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore
Floor 3 Posted 2010-08-18 17:40 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
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
Floor 4 Posted 2010-08-20 11:10 ·  中国 广东 东莞 电信
初级用户
Credits 175
Posts 45
Joined 2007-08-04 16:16
19-year member
UID 94640
Gender Male
Status Offline
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
Floor 5 Posted 2010-08-20 19:00 ·  中国 浙江 杭州 电信
新手上路
Credits 19
Posts 15
Joined 2010-08-20 13:09
15-year member
UID 172706
Gender Male
Status Offline
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 ]
Floor 6 Posted 2010-08-20 19:03 ·  中国 浙江 杭州 电信
新手上路
Credits 19
Posts 15
Joined 2010-08-20 13:09
15-year member
UID 172706
Gender Male
Status Offline
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 ]
Floor 7 Posted 2010-08-20 23:39 ·  中国 四川 泸州 联通
高级用户
★★★
Credits 609
Posts 374
Joined 2006-08-02 22:38
20-year member
UID 59720
Status Offline
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 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
hsyy +1 2010-08-21 11:24
Floor 8 Posted 2010-08-21 07:54 ·  中国 浙江 杭州 电信
新手上路
Credits 19
Posts 15
Joined 2010-08-20 13:09
15-year member
UID 172706
Gender Male
Status Offline
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.
Floor 9 Posted 2010-08-21 08:05 ·  中国 浙江 杭州 电信
新手上路
Credits 6
Posts 2
Joined 2010-08-21 08:00
15-year member
UID 172755
Gender Male
Status Offline
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 ]
Floor 10 Posted 2010-08-23 12:39 ·  中国 四川 南充 联通
高级用户
★★★
Credits 609
Posts 374
Joined 2006-08-02 22:38
20-year member
UID 59720
Status Offline
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.
Floor 11 Posted 2010-08-23 12:59 ·  中国 北京 263网络通信电信数据中心
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
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.
S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore
Floor 12 Posted 2010-08-23 13:53 ·  中国 浙江 杭州 电信
新手上路
Credits 19
Posts 15
Joined 2010-08-20 13:09
15-year member
UID 172706
Gender Male
Status Offline
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!
Floor 13 Posted 2010-08-23 15:18 ·  中国 浙江 杭州 电信
新手上路
Credits 19
Posts 15
Joined 2010-08-20 13:09
15-year member
UID 172706
Gender Male
Status Offline
@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!
Floor 14 Posted 2010-08-23 19:04 ·  中国 四川 南充 联通
高级用户
★★★
Credits 609
Posts 374
Joined 2006-08-02 22:38
20-year member
UID 59720
Status Offline
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, Credits to download 1 pts, Downloads: 4)
Floor 15 Posted 2010-08-24 11:18 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Basic knowledge of variable delayed expansion and variable nesting
Forum Jump: