Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   comando xor (https://www.clubdelphi.com/foros/showthread.php?t=79264)

JoseSagas 19-06-2012 22:55:53

comando xor
 
he estado buscando en internet y leyendo pero no entiendo por completo que es lo que hace xor alguien podria ayudarme con eso? gracias

Delphius 19-06-2012 23:10:26

¿Y que no entiendes de esto?
Es muy simple XOR, regresa verdadero cuando UNO y SOLO UNO de los operandos es verdadero pero no AMBOS. En otro caso es falso.

true XOR true = false
true XOR false = true
false XOR true = true
false XOR false = false

Saludos,

Casimiro Noteví 19-06-2012 23:15:32

Sólo tienes que pulsar F1-Ayuda y te sale el mensaje explicándolo ;)

Aquí un ejemplo:

Código Delphi [-]
var
  num1, num2, num3 : Integer;
  letter           : Char;

 begin
  num1   := $25;    // Binary value : 0010 0101   $25
  num2   := $32;    // Binary value : 0011 0010   $32
                    // XOr'ed value : 0001 0111 = $17
  letter := 'G';

  // And used to return a Boolean value
  if (num1 > 0) Xor (letter = 'G')
  then ShowMessage('Only one of the values is true')
  else ShowMessage('Both values are true or false');

  // And used to perform a mathematical Xor operation
  num3 := num1 Xor num2;

  // Display the result
  ShowMessageFmt('$25 Xor $32 = $%x',[num3]);
 end;

Cita:

The Xor keyword is used in two different ways:

1. To perform a logical or boolean 'Exclusive-or' of two logical values. If they are different, then the result is true.

2. To perform a mathematical 'Exclusive-or' of two integers. The result is a bitwise 'Exclusive-or' of the two numbers. For example:

10110001 Xor 01100110 = 11010111

Como ves, 1 xor 0 es 1, 0 xor 1 es 1, 0 xor 0 es 0 y 1 xor 1 es 1.


ecfisa 19-06-2012 23:20:47

Hola JoseSagas.

Agregando a lo dicho, aunque a xor es frecuente verlo relacionado con el cifrado de datos, también tiene otros usos:

Ejemplo de otro:
Código Delphi [-]
procedure Swap(var a,b: Integer);
begin
  a:= a xor b;
  b:= a xor b;
  a:= a xor b;
end;
...
var
  x,y: Integer;
begin
  x:= 5;
  y:= 12;
  Swap(x,y);
  ShowMessage(Format('%d %d',[x,y]));
...

Saludos.

JoseSagas 19-06-2012 23:36:13

gracias por la ayuda :)


La franja horaria es GMT +2. Ahora son las 04:19:54.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi