1.

Solve : [ask] About set /a ???

Answer»

i have read set command help (set /?) more time but i'am not enough understand about "set /a operator" :

i.e. quote from set /?
Quote

Two new switches have been added to the SET command:

SET /A expression
SET /P variable=[promptString]

The /A switch specifies that the string to the RIGHT of the equal sign
is a numerical expression that is evaluated. The expression evaluator
is pretty simple and SUPPORTS the following operations, in decreasing
order of precedence:

() - grouping
! ~ - - unary operators
* / % - arithmetic operators
+ - - arithmetic operators
<< >> - logical shift
& - bitwise and
^ - bitwise exclusive or
| - bitwise or
= *= /= %= += -= - assignment
&= ^= |= <<= >>=
, - expression separator

i not clearly about set /a operator like : |,^,&
anybody can give an example for using bitwise or "|" and bitwise exclusive or "^" on set /a command



thanks..

How can I convert a decimal number to binary, octal, hexadecimal?

Here is a way to convert a 8-bit decimal to its binary equivalent.
The formulation below emphasizes extracting each bit individually.

@echo off & setlocal enableextensions enabledelayedexpansion
set decimal_=%1
set /a b0=%decimal_% ^& 1
set /a b1=%decimal_% ^& 2
set /a b2=%decimal_% ^& 4
set /a b3=%decimal_% ^& 8
set /a b4=%decimal_% ^& 16
set /a b5=%decimal_% ^& 32
set /a b6=%decimal_% ^& 64
set /a b7=%decimal_% ^& 128
set i=-1
for %%b in (!b0! !b1! !b2! !b3! !b4! !b5! !b6! !b7!) do (
set bit=%%b
set /a i+=1
if !bit! EQU 0 (echo bit !i! = 0) else (echo bit !i! = 1)
)
endlocal & goto :EOF


An example of the output

bit 0 = 0
bit 1 = 0
bit 2 = 1
bit 3 = 1
bit 4 = 0
bit 5 = 0
bit 6 = 0
bit 7 = 0 thanks..it's great...
nice formula..i think

but, i am still not clearly, especially with "^&"
can u give explain that more basicly...
i.e. -->
- set /a "2^3" ; it return 1
- set /a "3^4" ; it return 7

what is the meaning of "^" ??
why 2^3=1 (3-2)....and 3^4=7 (3+4) ??


Some confusion because the ^ ("caret") character has 2 purposes...

(1) it is used to "escape" special characters in batch files, especially the & character, which breaks commands otherwise. For example to use a & b in a batch file you MUST type a ^& b. I know this is confusing but you just have to learn it if you need to do batch arithmentic!

(2) In batch arithmetic the ^ character is also the "bitwise exclusive or" operator.

A bitwise exclusive or takes two bit patterns of equal length and performs the logical XOR operation on each pair of corresponding bits. The result in each position is 1 if the two bits are different, and 0 if they are the same.

You can perform it manually like for school arithmetic as I shall demonstrate:-

Place an 0 in the answer if the bits are the same and a 1 if they are different

Quote
2 = 00000010
3 = 00000011
--------
result = 00000001 = 1

3 = 00000011
4 = 00000100
--------
result = 00000111 = 7

QED




hmmm...
yea, like a logical arithmetic lesson in school ...(maybe in Junior High School)
it's long time AGO...


littte mind about logical arithmetic
but, it an OR "|" operator..not XOR

[expression 1] or [expression 2] = Result
if expression 1 is true and expression 2 true , result will be True
if one of expression is False , result will be False

ilustrate on table:
Quote
----------------------
A B A or B
----------------------
T T T
T F F
F T F
F F F
note: T=True , F=False

is this same think as your demonstration ??Quote from: Fen_Li on October 01, 2007, 02:44:47 PM
is this same think as your demonstration ??

similar idea.
thanks so far for solved my problem...
that's great help...

maybe, I will do some TRIAL an error....to enhance experience..
coz, 'experience is the best teacher'

Regard...


Discussion

No Comment Found