#1 13 May 2018 10:48

green
Member
From: Великие Луки
Registered: 13 May 2018
Posts: 18

error in CMD SET

There are error in ss64.com/nt/set.html. Instead of One's complement, there are must be en.wikipedia.org/wiki/Two's_complement.
Check in CMD.

set /a e=~-11

Offline

#2 13 May 2018 11:48

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 938
Website

Re: error in CMD SET

This came up before in the thread here:
https://ss64.org/viewtopic.php?id=1649

Looking at it again I think the ~  is a Bitwise invert, it seems to be a buggy implementation of a bitwise NOT

SET /A arithmetic shift operators do not detect overflow and I think that's why it often reverses the  + / - sign of the result.

When you have a combination of undocumented and buggy behaviour like this, I think it's probably best to just avoid using that 'feature'

Offline

#3 13 May 2018 11:55

green
Member
From: Великие Луки
Registered: 13 May 2018
Posts: 18

Re: error in CMD SET

There are no error, if change One's complement to Two's complement. Prove I'm wrong.

Offline

#4 13 May 2018 12:23

green
Member
From: Великие Луки
Registered: 13 May 2018
Posts: 18

Re: error in CMD SET

There are no bugs in SET command. It works totally correct, according to Two's complement. So, change your article, replacing One's complement link to Two's complement link.

Offline

#5 13 May 2018 12:27

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 938
Website

Re: error in CMD SET

Set /a "~-4"

returns
3

Thats not a Two's complement

Offline

#6 13 May 2018 12:37

green
Member
From: Великие Луки
Registered: 13 May 2018
Posts: 18

Re: error in CMD SET

You know math not good, I see.
SET uses 32-bit values. Let's remember school and calculate together.
-4 in binary is 1111 1111 1111 1111 1111 1111 1111 1100
~ is bitwise negation, so number above inverts every bit. Compare initial value and resuilt below.
1111 1111 1111 1111 1111 1111 1111 1100
0000 0000 0000 0000 0000 0000 0000 0011
It gives 3. questions?

Offline

#7 13 May 2018 12:39

green
Member
From: Великие Луки
Registered: 13 May 2018
Posts: 18

Re: error in CMD SET

1111 1111 1111 1111 1111 1111 1111 1100
0000 0000 0000 0000 0000 0000 0000 0011

more comfortable format for comparison

Offline

#8 13 May 2018 12:49

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 938
Website

Re: error in CMD SET

Yes it is bitwise negation, not ones complement or twos complement

A couple more examples
Set /a "~0"
-1
Set /a "~4"
-5

Offline

#9 13 May 2018 14:09

green
Member
From: Великие Луки
Registered: 13 May 2018
Posts: 18

Re: error in CMD SET

0 in binary, and it's inversion are

0000 0000 0000 0000 0000 0000 0000 0000
1111 1111 1111 1111 1111 1111 1111 1111

1111 1111 1111 1111 1111 1111 1111 1111 is -1, isn't it?
4 in binary, and it's inversion are

0000 0000 0000 0000 0000 0000 0000 0100
1111 1111 1111 1111 1111 1111 1111 1011

1111 1111 1111 1111 1111 1111 1111 1011 is -5, isn't it?
Where is the mistake?

Offline

#10 13 May 2018 14:18

green
Member
From: Великие Луки
Registered: 13 May 2018
Posts: 18

Re: error in CMD SET

It's not a computed two's complement. I mean, that the number represented according to Two's complement form. Just it.

Offline

#11 13 May 2018 15:18

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 938
Website

Re: error in CMD SET

If you look at this page which explains how to calculate Twos complement
https://www.cs.cornell.edu/~tomf/notes/ … scomp.html
"To get the two's complement negative notation of an integer, you write out the number in binary. You then invert the digits, and add one to the result".

If you miss out that last stage, adding 1, which is what SET /A ~ is doing, thats aalllmost a 2's complement, but not quite, maybe I'm splitting hairs here but that's why I described it as a Bitwise invert rather than a 2's complement.

A Two's complement of 0 should be 0
as on the Wikipedia examples https://en.wikipedia.org/wiki/Two%27s_complement

Offline

#12 13 May 2018 20:45

green
Member
From: Великие Луки
Registered: 13 May 2018
Posts: 18

Re: error in CMD SET

Yes, you're right. And I confirmed it in my previous post.

green wrote:

It's not a computed two's complement.

I mean just, that the number is represented according to Two's complement scheme, which describes, how numbers are being stored and calculated in a processor. And I wanted to say, that the SET command not buggy, not in the least. It works as declared. Am I right?

Offline

#13 14 May 2018 18:19

green
Member
From: Великие Луки
Registered: 13 May 2018
Posts: 18

Re: error in CMD SET

The article has been altered, I see, but one error has been replaced by other. Now in the article buggy implementation of a bitwise NOT text placed. But words buggy implementation of a are erroneous, especially word buggy, because SET command works exactly as bitwise NOT. Why you write buggy?

Offline

#14 14 May 2018 18:50

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 938
Website

Re: error in CMD SET

OK I have edited it again and removed the buggy description, the Microsoft docs just describe it as a 'unary operator' which is vague enough to cover many things.

Thanks for flagging this up by the way, it is nice to get these little details correct even if hardly anyone uses them.

Offline

#15 14 May 2018 23:11

green
Member
From: Великие Луки
Registered: 13 May 2018
Posts: 18

Re: error in CMD SET

Thanks for understanding. By the way, another mistake (#2) was noticed by me, when I found this article for the first time. I write very complex scripts, which must execute fastest way, so I need to use every trifle of interpreter/compiler behaviour. So, I need to know that behaviour thoroughly. I have noticed the mistake #2, but haven't wrote to you at that time. But this mistake (#1, with SET) made me angry, and I have registered on the forum for sake of the mistake #1 correction. Now I have opportunity for mistake #2 correction. It's description below.
Instead of %%= needs to be %= (second Modulus operator).

P.S. Your site is cool. Most detailed documentation and simplicity combined. Keep it up!

Last edited by green (14 May 2018 23:21)

Offline

#16 14 May 2018 23:27

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 938
Website

Re: error in CMD SET

Are you running this at the command line or in a batch file?

The modulus % operator, like FOR parameters has to be doubled to %% inside a batch file, I need to add a note about this to the page.

Offline

#17 14 May 2018 23:51

green
Member
From: Великие Луки
Registered: 13 May 2018
Posts: 18

Re: error in CMD SET

You re right now. Of course I know about % doubling. But your mistake is obvious, because in the first Modulus operator you write single percent, and in the second (on the next line) you write doubled percent. Indeed, you need to write canonical form in the documentation, that is, of course, single char in the both cases. But below (or at the beginning of the article) a note about command line parsing should be added, which must contain link to another article about differences in command parsing by interpreter: in a command prompt, and in a script file.

Offline

#18 14 May 2018 23:59

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 938
Website

Re: error in CMD SET

Yes, I've fixed that now, if you refresh the page.

In fact there was already a note about the doubling halfway down, but at some point I moved the /A syntax to the top of the page. I have now put it back to the original location, next to the examples and made the %'s consistent.

Thanks again

Offline

#19 15 May 2018 00:15

green
Member
From: Великие Луки
Registered: 13 May 2018
Posts: 18

Re: error in CMD SET

It's better now. But, if to be correct, not Modulus operator (by the way, in your current version you need to write not only about first Modulus operator, but about second Modulus operator too, which converts to %%= in a batch file) must be doubled, but every literal PERCENT char must be doubled, no matter, operator it is, or part of operator, or somewhat else. What matters, is just number of in-script evaluate operations. For example, if in a script a substring with %%%% will be evaluated twice, it gives literal %. And if you'll try to evaluate it for the third time, you, probably, will get an error.

Offline

#20 15 May 2018 02:41

Pyprohly
Member
Registered: 26 Nov 2014
Posts: 24

Re: error in CMD SET

Green’s initial suggestion to change “one’s complement” to “two’s complement” wouldn’t make the description of the bitwise not operator more accurate than it already was.

The bitwise not/invert operator simply flips bits (one’s complement—“two’s complement minus one”), but the result is interpreted as a two’s complement number. It would make sense to mention one’s complement, but not two’s complement, as one’s complement relates closely to what the operator is doing.

I think the best description to display here would be “Bitwise negation (one's complement)”, similar to what it was originally.

@green I don’t think that mentioning the general batch rule that all literal percent symbols must be doubled is a better alternative for what is currently written. It’s not really relevant to the set command, and using literal percents as anything other than modulo in an arithmetic expression doesn’t make sense.


Since we’re taking amendments, next to the subtraction operator in parenthesises it mentions “unary”. Unary is characteristic of the operator in its unary form but its name is “negation”.

Offline

#21 15 May 2018 09:10

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 938
Website

Re: error in CMD SET

Pyprohly wrote:

The bitwise not/invert operator simply flips bits (one’s complement—“two’s complement minus one”), but the result is interpreted as a two’s complement number.

Im not quite sure I understand what you mean by that, as far as I can see it simply flips the bits and then returns the value as a signed decimal, so I don't think it's accurate enough to describe it as either One or Two's complement.

Adding one to the value returned would give you the twos complement, and that seems like the most likely use case to me. Two's complement is more widely used.

Offline

#22 15 May 2018 09:20

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 938
Website

Re: error in CMD SET

Also for the thing about escaping %%, Rob van der Woude has a page on escapes which states:

"%%     May not always be required in doublequoted strings, just try"
http://www.robvanderwoude.com/escapechars.php

However I've not been able to reproduce that.

Offline

#23 15 May 2018 10:43

green
Member
From: Великие Луки
Registered: 13 May 2018
Posts: 18

Re: error in CMD SET

As far, as I know, and all my experience proves it, if you want to get literal % in a script execution, percent char ALWAYS must be doubled, because it evaluates before double quotes. Proof below.

set var="
echo %var% this text won't be redirected to > file %var%

Offline

#24 15 May 2018 11:53

Pyprohly
Member
Registered: 26 Nov 2014
Posts: 24

Re: error in CMD SET

Simon Sheppard wrote:

I don't think it's accurate enough to describe it as either One or Two's complement.

Yet, the set page is currently describing it as “Two’s complement minus 1”? That’s a silly thing to say because two's complement minus one is one’s complement; two’s complement is one’s complement plus one.

One’s complement is what green has been described and demonstrated in this thread, although he mistakes it for two’s complement.


The comment from Rob van der Woude’s site is likely one that come from observing the output of a command entered on the command prompt. An extra percent symbol is invariably needed to produce a literal percent symbol in a batch file because percent expansion is evaluated as one of the very first stages of the batch line parser.

green wrote:

As far, as I know, and all my experience proves it

Inductive reasoning cannot be used to provide proofs, only reasons to believe that that it is likely true.

Offline

#25 15 May 2018 13:11

green
Member
From: Великие Луки
Registered: 13 May 2018
Posts: 18

Re: error in CMD SET

One's complement may mislead unexperienced users, because they try ~10 and get -11, when -10 expected, or  they try ~-10 and get 9. May be, better not to give second value of ~ operator applying (complement-related), and first value (bitwise invert) quite enough. But remark about number machine representation and calculation should be added, and that remark will be, certainly, about Two's complement.
I know English not good. May be, the sentence I wrote, be better, if like below? Or that sentence will have same meaning?
As far, as I know, if you want to get literal % in a script execution, percent char ALWAYS must be doubled, because it evaluates before double quotes, and all my experience proves it.

Offline

#26 15 May 2018 18:35

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 938
Website

Re: error in CMD SET

Pyprohly wrote:

two’s complement is one’s complement plus one.

I think thats only correct for positive numbers, compare the values in the table here:

https://en.wikipedia.org/wiki/Signed_number_representations#Ones'_complement

The negative values all differ by 1, but the positive values are the same

01111110     126
01111111     127

Offline

#27 16 May 2018 00:55

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 938
Website

Re: error in CMD SET

I've now added a page about the % symbols
https://ss64.com/nt/syntax-percent.html

Offline

#28 16 May 2018 02:53

Pyprohly
Member
Registered: 26 Nov 2014
Posts: 24

Re: error in CMD SET

How one and two’s complement represents a number is of subordinate value here. You must be careful about the distinction between representation and the process of one/two’s complement. If we’re interested in understanding how the bitwise not operator performs a transformation then it is how the process of one and two’s complement is used to manipulate a number that we are mostly concerned with.

A positive number in binary, one’s complement binary, and two’s complement binary is the same. An interesting consequence. But only incidental to how bitwise inversion works.

In terms of how bitwise inversion works, it is not finding the the one’s or two’s complement of a number, rather it is performing ones/two’s complement on a number (specifically one’s complement). It’s the difference between representation and process, and we’re more interested in the process.

So, returning to “two’s complement is one’s complement plus one”, I’m talking about the process of one and two’s complement transformations, not how one or two’s complement is used to represent numbers. Hence, the data in the indicated table is not relevant, and it follows that the conclusion—that it is “only correct for positive numbers”—isn’t.

Offline

#29 17 May 2018 01:05

green
Member
From: Великие Луки
Registered: 13 May 2018
Posts: 18

Re: error in CMD SET

Firstly, I wrote (aspect A)...

But, if to be correct, not Modulus operator must be doubled, but every literal PERCENT char must be doubled, no matter, operator it is, or part of operator, or somewhat else.

And also (aspect B)...

by the way, in your current version you need to write not only about first Modulus operator, but about second Modulus operator too, which converts to %%= in a batch file

So, I had assumed, you were choose one of the aspects for correction, corrected it, and deleted info about other aspect, or you were corrected both. But you just had ignored my post, and hadn't made any corrections of these aspects. For clearer aspect B understanding: first Modulus operator consist of one percent char, and second Modulus operator consist of two consequent chars: percent and equal sign.

Secondly, in a new article about percent symbols. There are no need to write about FOR at he beginning at all. Instead of it, there (second marker from the beginning, Next ... from 1 to 5.) must be remark about SET variables (e.g. %variable%), because FOR uses just literal %, and have no relation to in-script percent char evaluation. And further some correction...

When working at the command line (not in a batch script) there is no possibility of any batch file parameters %1, %2 etc so the logic above is not followed and hence FOR parameters on the command line only need a single %.

It's needed to complement this sentence with remark about SET variables, but not only about script numbered parameters (batch file parameters), as it stated now.

Don't use numbers as FOR parameter variables, it may work but it will confuse people (and is undocumented).

This item is totally useless and may mislead users.

Overall about second question, it's important to understand, that evaluation of FOR operator (evaluation A) takes place after evaluation of percent char (for only two purposes: script numbered parameters, and SET variables, it's evaluation B), and these evaluations (A and B) have no other relations, except sequence.

Offline

#30 17 May 2018 20:41

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 938
Website

Re: error in CMD SET

Sorry I've been kinda busy this week, I had to read your message several times to get it but now I see what you mean.

I was assuming the SET /A % and %% were just the batch file and command line versions of a modulus function, but now I see they return completely different results when used in a batch file

In a batch file:
17%%5
gives 2

but 17%5
gives 17

and 17%20
gives 170

However if I also send the parameter %2 to the batch file when Im testing this, then the numbers change:
pass 100 as %2
now 17%20
gives 171000

So I think this is a bug and the only valid modulus parameter for SET /A is %%

Offline

Board footer

Powered by FluxBB