Skocz do zawartości

[Delphi] Ciekawostka odnosnie shiftowania


Toster

Polecane posty

Pytanie jest proste jak zawsze a wyglada tak

var
 tmp1, tmp2: System.UInt64;
begin
  tmp1 := 1 shl 1;
  tmp2 := 1 shl 33;
  Assert( tmp1 <> tmp2 );
end;

 

I teraz pytanie: Bedzie assert (zmienne sa rowne) czy nie bedzie asserta (zmienne sa rozne) ?

 

Oczywiscie najpierw odpowiedz pozniej sprawdz :)

Always Dark<br />u1_tt_logo.png banner-1.pngexFabula-banner.pngson_banner_ubersmall.jpg

Link do komentarza
Udostępnij na innych stronach

chmy.. tu jest jakiś haczyk bo wygląda, że nie będzie asserta, ale wtedy byś nie pytał, więc moje przypuszczenie jest takie, że liczby 64 bitowe Delphi emuluje na procesorze 32 bitowym, ale nie idą mu przesunięcia bitowe i to o ile chce się przesunąć bierze modulo 32. Nie sprawdzałem, nie wiem czy mądrze gadam, pozory próbuję sprawić :D

Baza tysięcy lotnisk: http://airportsbase.com

Link do komentarza
Udostępnij na innych stronach

otoz ja tez sie zdziwilem, bo okazalo sie ze shl dziala jak w asmie rol a nie shl. Czyli mowiac jasniej dziala jak przesuniecie z przeniesieniem, to co wychodzi z lewej wchodzi z prawej.

 

co ciekawsze shl dziala na 32 bitach, tak wiec sytuacja opisana powyzej 1 shl 33 = 2, przynajmniej tak mi wyszlo na TD.

Za nic nie moglem go zmusic aby rolowal 64 bity, w dokumentacji jest tylko niejasna wzmianka ze rejestry sa 32 bitowe i nara.

 

Wiecej ciekawostek przy czyms takim:

var
 tmp1: System.UInt64;
 t: integer;
 tmp2: cardinal;
 tmp3: integer;
begin
  tmp1 := 1;
  tmp2 := 1;
  tmp3 := 1;
  Writeln('UInt64');
  for t := 0 to 32 do begin
     tmp1 := 1 shl t;
     Writeln('   1 shl '+IntToStr(t)+' = '+IntToStr(tmp1));
  end;

  Writeln('Cardinal');
  for t := 0 to 32 do begin
     tmp2 := 1 shl t;
     Writeln('   1 shl '+IntToStr(t)+' = '+IntToStr(tmp2));
  end;

  Writeln('Integer');
  for t := 0 to 32 do begin
     tmp3 := 1 shl t;
     Writeln('   1 shl '+IntToStr(t)+' = '+IntToStr(tmp3));
  end;
  readln;
end.

 

wynik:

 

UInt64

1 shl 0 = 1

1 shl 1 = 2

1 shl 2 = 4

1 shl 3 = 8

1 shl 4 = 16

1 shl 5 = 32

1 shl 6 = 64

1 shl 7 = 128

1 shl 8 = 256

1 shl 9 = 512

1 shl 10 = 1024

1 shl 11 = 2048

1 shl 12 = 4096

1 shl 13 = 8192

1 shl 14 = 16384

1 shl 15 = 32768

1 shl 16 = 65536

1 shl 17 = 131072

1 shl 18 = 262144

1 shl 19 = 524288

1 shl 20 = 1048576

1 shl 21 = 2097152

1 shl 22 = 4194304

1 shl 23 = 8388608

1 shl 24 = 16777216

1 shl 25 = 33554432

1 shl 26 = 67108864

1 shl 27 = 134217728

1 shl 28 = 268435456

1 shl 29 = 536870912

1 shl 30 = 1073741824

1 shl 31 = -2147483648

1 shl 32 = 1

Cardinal

1 shl 0 = 1

1 shl 1 = 2

1 shl 2 = 4

1 shl 3 = 8

1 shl 4 = 16

1 shl 5 = 32

1 shl 6 = 64

1 shl 7 = 128

1 shl 8 = 256

1 shl 9 = 512

1 shl 10 = 1024

1 shl 11 = 2048

1 shl 12 = 4096

1 shl 13 = 8192

1 shl 14 = 16384

1 shl 15 = 32768

1 shl 16 = 65536

1 shl 17 = 131072

1 shl 18 = 262144

1 shl 19 = 524288

1 shl 20 = 1048576

1 shl 21 = 2097152

1 shl 22 = 4194304

1 shl 23 = 8388608

1 shl 24 = 16777216

1 shl 25 = 33554432

1 shl 26 = 67108864

1 shl 27 = 134217728

1 shl 28 = 268435456

1 shl 29 = 536870912

1 shl 30 = 1073741824

1 shl 31 = 2147483648

1 shl 32 = 1

Integer

1 shl 0 = 1

1 shl 1 = 2

1 shl 2 = 4

1 shl 3 = 8

1 shl 4 = 16

1 shl 5 = 32

1 shl 6 = 64

1 shl 7 = 128

1 shl 8 = 256

1 shl 9 = 512

1 shl 10 = 1024

1 shl 11 = 2048

1 shl 12 = 4096

1 shl 13 = 8192

1 shl 14 = 16384

1 shl 15 = 32768

1 shl 16 = 65536

1 shl 17 = 131072

1 shl 18 = 262144

1 shl 19 = 524288

1 shl 20 = 1048576

1 shl 21 = 2097152

1 shl 22 = 4194304

1 shl 23 = 8388608

1 shl 24 = 16777216

1 shl 25 = 33554432

1 shl 26 = 67108864

1 shl 27 = 134217728

1 shl 28 = 268435456

1 shl 29 = 536870912

1 shl 30 = 1073741824

1 shl 31 = -2147483648

1 shl 32 = 1

Always Dark<br />u1_tt_logo.png banner-1.pngexFabula-banner.pngson_banner_ubersmall.jpg

Link do komentarza
Udostępnij na innych stronach

Acha, trafiłem :D chyba się sam nagrodzę :P

Edit: wg mnie sie mylisz z tym rollem, myślę, że moja wersja z zawijaniem tego co jest po prawej modulo 32 jest dobra, może sprawdź na liczbię na przykład 107, bitowo to jest 1101011, to wtedy zobaczy się, czy zawija, wg mnie nie, bo to by było głupie

Baza tysięcy lotnisk: http://airportsbase.com

Link do komentarza
Udostępnij na innych stronach

Zarchiwizowany

Ten temat jest archiwizowany i nie można dodawać nowych odpowiedzi.

×
×
  • Utwórz nowe...