Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   bit <---> word (https://www.delphipraxis.net/57152-bit-word.html)

TeronG 17. Nov 2005 11:38

Re: bit <---> word
 
ahh ... cool danke :)

Die SHL-functionen von DAX gefallen mir aber die BitsToInt scheint nicht zu klappen :gruebel:

EDIT: ahh .. wenn ich die bit's umdrehe dann klapps (31=0, 30=1,...)
Delphi-Quellcode:
for i := High(Bits) downto 0 do
habs einfach so gemach .. dann klappts wieder ^^
jetzt nur noch rausfinden warum's überhaupt geht :gruebel:

negaH 17. Nov 2005 12:39

Re: bit <---> word
 
Zitat:

Zitat von Dax
Das geht aber viel einfacher ;)

Delphi-Quellcode:
function BitsToInt(Bits: array of Boolean): Integer;
var i: Integer;
begin
  Result := 0;
  for i := 0 to High(Bits) do
  begin
    if Bits[i] then
      Result := Result shl 1 or 1
    else
      Result := Result shl 1;
  end;
end;

und noch einfacher

Delphi-Quellcode:
begin
  Result := 0;
  for i := Low(Bits) to High(Bits) do
    Inc(Result, Result + Ord(Bits[i]));
end;
Gruß Hagen

TeronG 17. Nov 2005 12:51

Re: bit <---> word
 
hier meine ^^ (die versteh ich sogar !!)
Delphi-Quellcode:
function TForm1.BitToWord(bits:TBitArray):Word;
var
  i : Integer;
begin
  result := 0;
  for i := low(bits) to high(bits) do if bits[i] then result := result + (1 shl i);
end;

function TForm1.WordToBit(eingang:Word): TBitArray;
var
  m,i : integer;
begin
  for i := 15 downto 0 do begin
    m:= 1 shl i;
    if eingang >= m then begin
      result[i] := true;
      eingang := eingang-m;
    end else result[i] := false;
  end
end;
Dank shl ganz ohne power(2,y), trunc, div & mod !!

TeronG 31. Mai 2007 11:46

Re: bit <---> word
 
Dank himitsu's Post ist mir ne "bessere" Version von meinem 'WordToBit' eingefallen:

Delphi-Quellcode:
type TBitArray = array[0..15] of boolean;

...

function WordToBit(Eingang:Word): TBitArray;
var
  i : integer;
begin
  for i := 0 to 15 do Result[i] := (Eingang shr i) and 1 <> 0;
end;

Robert Marquardt 31. Mai 2007 12:20

Re: bit <---> word
 
JCL to the rescue! JclLogic.pas hat alles was du brauchst und noch viel mehr.

DP-Maintenance 31. Mai 2007 12:55

DP-Maintenance
 
Dieses Thema wurde von "Matze" von "Programmieren allgemein" nach "Sonstige Fragen zu Delphi" verschoben.


Alle Zeitangaben in WEZ +1. Es ist jetzt 07:24 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz