Autor Beitrag
Coder
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1383
Erhaltene Danke: 1

WinXP
D2005 PE
BeitragVerfasst: Do 11.01.07 01:42 
Hi
Wie kann ich prüfen ob eine Zahl eine Potenz von 2 ist.
Lässt sich irgendwie schwer nach dem Thema suchen. :(

MfG
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Do 11.01.07 01:43 
Moin!

Zähl die Bits, ist nur eins gesetzt, ist es eine 2er-Potenz. ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Coder Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1383
Erhaltene Danke: 1

WinXP
D2005 PE
BeitragVerfasst: Do 11.01.07 01:49 
Wohow wie mach ich das denn? :shock:
Length geht nicht :gruebel: :lol:

MfG
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Do 11.01.07 01:56 
Moin!

Die Suche hilft: Suche in: Delphi-Forum, Delphi-Library EINSEN ZÄHLEN :D

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Coder Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1383
Erhaltene Danke: 1

WinXP
D2005 PE
BeitragVerfasst: Do 11.01.07 02:17 
Danke!
Hab jetzt diese Funktion gefunden.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
function IsPowerOfTwo(d : dword) : boolean;
begin
  Result := (d>0) and (d and (d - 1) = 0);
end;

Ich hab zwar keine Ahnung wie das funktioniert aber es geht. :lupe: :nixweiss:
Statt dword kann man auch Integer nehmen.

MfG
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Do 11.01.07 11:09 
Beispiel:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
d         = 0010000 = 16
d-1       = 0001111 = 15
d and d-1 = 0000000 = 0    --> (d > 0) und ((d and d - 1) = 0)

d         = 0110000 = 48
d-1       = 0101111 = 47
d and d-1 = 0100000 = 32   --> (d and d - 1) ist nicht gleich 0

Der Trick ist, dass bei einer Subtraktion von 1 von einer Zahl, bei der nur ein Bit gesetzt ist, danach alle Bits rechts davon (niederwertigere eben) 1 gesetzt und das ursrünglich gesetzte nicht mehr gesetzt ist.
Ist jetzt noch ein anderes Bit gesetzt, dann wird eben nur bis zum ersten alles 1 und das andere bleibt unverändert. Dann ist also dieses weitere Bit in d und in d-1 gesetzt. Also ist es auch in der Verundung drin, und die ist nicht 0...
Coder Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1383
Erhaltene Danke: 1

WinXP
D2005 PE
BeitragVerfasst: Do 11.01.07 21:28 
Achso verstehe!
Das letzte and ist kein boolscher operator?

MfG
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Do 11.01.07 21:32 
Das ist letztendlich dasselbe... ;-)
Der Unterschied ist lediglich, dass das zweite and bitweise auf alle Bits der Variablen angewendet wird. Aber nimm mal einfach True als 1 und False als 0, und schon bist du auf Bitebene...
Allesquarks
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 510

Win XP Prof
Delphi 7 E
BeitragVerfasst: Fr 12.01.07 11:06 
Dafür gab es glaube ich direkt nen Assembler Befehl. Die Liste liegt aber gerade zu Hause werde nachher mal nachschauen.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 12.01.07 23:08 
Klar gibts für and nen Assemblerbefehl: and... ;-)