﻿These are some discussions that arose through the development of Challenge0014:

Message:
Date: Mon, 5 Mar 2001 16:39:58 -0500
From: "José Luis De la Cruz Lázaro" <jcruz@ec-red.com>
Subject: [The Challenge] 0014 - Final

This message is only for Nyder:

I just want you to explain one thing to me, which you took for granted, it is true that
at some point you had to exchange the values ​​of the variables a and b
without an auxiliary variable, for this you chose the bucket algorithm
which was explained in this list some time ago ), namely:

a = a+b; //is there a mistake here?
b = a-b;
a = a-b;

As we know, a+b can contain values ​​greater than the maximum allowed
by the int type, therefore 1 bit is lost when doing this sum and assigning
this value to "a", however, when doing b = a-b, it seems as if this effect had not
occurred, that is, b contains the value of a, and therefore the
program runs satisfactorily. How do you explain this?

In the next message I will send the solution JUDGE.

Anyway, as the winner of this challenge, now it is your turn to propose the
next Challenge ( 0015 ). I hope your challenge is on the list.

Regards

       o  o Jose Luis De la Cruz Lazaro   o   220KV of Chaos
     o       o  Visit my homepage:          o      o
   o    o o    o THE WORLD OF CHAOS           o   o o
  o   o  o     o   https://www.theworldofchaos.com   o  o
  o    o     o                                   o    o  o
   o     o o     Chaos = Chaos & math ? C++ : ++C;        o
     o                                                     o
        o  o  o o o  FRACTALS UNLIMITED ooo o  o  o  o  o   o
                    o  o   o   o   o   o
     o               o   o   o   o   o
   o   o      o
 o      o   o  o  o  o  o o oooo      Yacsha Software & Desing
                                 O  O  o ooo Lima - Peru ooo o o O  O



Reply :

Message:
Date: Tue, 6 Mar 2001 00:16:00 -0600
From: Nyder Menéndez <nyder2000@yahoo.es>
Subject: Re: [The Challenge] 0014 - Final --->Reply...

Greetings...
(Although it may seem redundant... :-)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>I just want you to explain one thing to me, which you took for granted, it is
>true that at some point you had to exchange the values ​​of
>the variables a and b without an auxiliary variable, for this you chose
>the bucket algorithm that was explained in this list some time
>ago), namely:

>a = a+b; //is there a mistake here?
>b = a-b;
>a = a-b;

>As we know that a+b can contain values ​​greater than the maximum
>allowed by the int type, therefore 1 bit is lost when doing
>this sum and assigning this value to "a", however when doing b = a-b,
>it seems as if this effect had not occurred, that is, b contains
>the value of a, and therefore the program runs satisfactorily.
>How do you explain this?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~
Good.. . I don't know if I understood the question well...
but let's try to explain it...

(I'm going to try to explain it with numbers...
I mean... mathematically...
then I hope you explain it to me...
in bits... because I think you understand that...
better than me... :-)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Let's suppose, (to make it simpler)...
that the integers had a range from -21 to 20

and let's give two arbitrary numbers... 15 and 8

that are within the range from -21 to 20...
but whose sum... falls outside this range...

(as we remember C does not control this type of...
situations... ...this is up to the programmer...
i.e.... us... :-)

When the program "adds" both values... and "hits"...
the maximum value allowed...
then the program "does" the following...

(as far as I can understand...)

int a,b;
a=15;
b=8;

a=a+b;

// a= 15 + (........................8........................)
// a= 15... +1 +1 +1 +1 +1 +1 +1 +1
// 16... 17... 18... 19... 20... -21... -20... -19
//
// then...
// a would be equal to -19

b=a-b;

// b= -19 - (.....................8........................)
// b= -19... -1 -1 -1 -1 -1 -1 -1 -1
// -20... -21... 20... 19... 18... 17... 16... 15
//
// then...
// b would be equal to 15... which was the original value of "a"...
// which corrects... the "error" caused by the "overflow"...
// if you can call it that...

// and of course... with...

a=a-b;

// the same thing would happen... only it would be... a little longer...
// and if you want you can check it...
// finally leaving "a" with a value of 8...
// which was the previous value of b...

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This would be "my explanation"... within the logic...
as I see it...

Now... I would like to see... the explanation... with the bits...

:-)

By the way... I haven't tried the solution yet...
but I'm going to look at it in a few moments...
to learn what XOR is all about...
(for me it's new material... even though it's been around for a long time... :-)

but I love learning...
having new tools to...
apply them to the logics...
that one has to develop...

nyder2000@yahoo.es

Reply:

Message:
Date: Tue, 6 Mar 2001 10:31:31 -0500
From: "José Luis De la Cruz Lázaro" <jcruz@ec-red.com>
Subject: RE: [The Challenge] 0014 - Reply to Nyder

Hello Nyder

> From: Nyder Menéndez <nyder2000@yahoo.es>
>
> This would be "my explanation"... within logic...
> as I see it...

Your explanation was excellent :-) , very good research on the
internal treatment of integers.

> Now... I would like to see... the explanation... with the bits...
>
> :-)

Since you ask, I will give the answer but from the binary point of view:

First of all this is not only true in C, it is true in any digital electronic system, processors are designed to work with this
convention and therefore the software must adapt to this situation.

To make the explanation easier, let's suppose that an "int" type has 5
bits, then we have that the most important bit will be the sign bit ( 0 =
"+" , 1 = "-" ), this means that the whole number itself,
would only be represented with 15 bits.

For example the number 13 is defined binary as:

0 1101
| \ /
| number
+--- positive sign

But for -13 it will be defined as follows:

1 0011
| \ /
| number
+--- negative sign

It is observed that: number = 0011 = 3 = 16 - 13 = 2^4 - 13

This is why this convention is called "2's Complement", because the
negative proper number turns out to be the arithmetic complement with respect
to the next higher power of 2.

This explains why the range of integers in this case will be
from -16 to 15 and not from -15 to 16.

After this brief introduction to the binary integer representation,
I will be able to explain why the apparent overflow error does not occur in the
operation: a = (a + b) - b;

Assuming a=15 and b=3 of the "int" type (5 bits) defined above,
then:

When applying the binary sum (it's like adding in base 10, but the carry will be
a binary):

15 = 01111
3 = 00011 +
-------
10010 = 18?

With which we see that the sum does work, because if we were using
an "int" type greater than 5 bits, 10010 = 18 = 15 + 3. But it is not so, since
from what we explained above 10010 = -14.

Then when applying the binary difference:

-14 = 10010
3 = 00011 -
-------
01111 = 15

With which we observe that everything returns to normal again.

In conclusion, it can be said that negative numbers in an "int" type,
represent the binary continuation of positive numbers, behaving
internally like an "unsigned int".

I hope this little explanation has satisfied your concern.

> By the way... I haven't tried the solution yet...
> but I'll look into it in a few moments...
> to learn what XOR is all about...
> (for me it's new material... even though it's been around for a long time... :-)

XOR has many applications to software and hardware, and its main
importance lies in the fact that it is both a direct and inverse operator, for
example:

(7^15)^7 = 15 which is analogous to: (7+15)-7 =15

This peculiarity gives greater speed to the numerical calculations that the
processor makes, that's why when you want to use masks you prefer to use XOR, instead of arithmetic operations that take
more time and also produce overflows.

For example, a well-known mask is present when you move
a window in MS Windows. It is true that when you move the window, it appears
represented only by its frame, which is a simple rectangle. If you look closely,
you will see that the rectangle's pixels take the inverse color of the place
where they are, that is, they use the color of the screen as a mask. This
is an old trick that serves to always display said rectangle
regardless of the color of its background. And this is done with a simple XOR:

Where "mask" is a "control" color, which will control that the dispersion
of the color tone does not vary much with respect to "mask". For the example,
mask can be the color gray.

color = mask^color //find the color of the rectangle's pixel
//paint the "color" of the pixel
color = mask^color //recover the color of the screen to paint it
after the rectangle is moved from its position.

Regards
Good Luck

       o  o Jose Luis De la Cruz Lazaro   o   220KV of Chaos
     o       o  Visit my homepage:          o      o
   o    o o    o THE WORLD OF CHAOS           o   o o
  o   o  o     o   https://www.theworldofchaos.com   o  o
  o    o     o                                   o    o  o
   o     o o     Chaos = Chaos & math ? C++ : ++C;        o
     o                                                     o
        o  o  o o o  FRACTALS UNLIMITED ooo o  o  o  o  o   o
                    o  o   o   o   o   o
     o               o   o   o   o   o
   o   o      o
 o      o   o  o  o  o  o o oooo      Yacsha Software & Desing
                                 O  O  o ooo Lima - Peru ooo o o O  O





