Autor Beitrag
rizla
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 417
Erhaltene Danke: 2

XP
FPC mit Lazarus
BeitragVerfasst: Do 09.12.10 18:21 
Hi,
irgendwie steht ich gerade auf dem Schlauch :x
Wie setze ich aus [x] bits ein Byte zusammen? (Maximal 8 Bit - sonst wäre es ja mehr als ein Byte :D)

Ich habe z.B. 5 Bytes, daraus nehm ich jeweils das erste Bit (lsb) und diese will ich zu einem Byte "b" zusammenfassen:

Beispiel:

Byte 1 = (00000011)
Byte 2 = (00000001)
Byte 3 = (00000000)
Byte 4 = (00000001)
Byte 5 = (00000000)

b = 11010 (resp. 00011010)

Habe gedacht, ich setze jeweils das erste Bit auf 0 oder 1 und shifte dann eine Stelle nach links , aber das funktioniert nicht..

Hilfe!

Danke im Vorraus.

rizla

_________________
if you have what they want - they'll find a way to take it (bruce sterling)
WOW - 10 JAHRE Mitglied beim Delphi-Forum. Wie die Zeit vergeht, Freunde.
Flamefire
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1207
Erhaltene Danke: 31

Win 10
Delphi 2009 Pro, C++ (Visual Studio)
BeitragVerfasst: Do 09.12.10 18:25 
Ich bin dafür jeden zu steinigen, der "geht nicht" schreibt. :twisted:

Also: Was geht nicht? Welchen Code hast du?
rizla Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 417
Erhaltene Danke: 2

XP
FPC mit Lazarus
BeitragVerfasst: Do 09.12.10 18:41 
Globale Variable PalIdx: byte;


Funktion, um ein Bit zu lesen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
function TForm1.GetBit: byte;
begin
 result := 0;
 if CurrBit = 8 then
  begin
   CurrBit := 0;
   inc(CurrOffset);
  end;
  if (Buffer[CurrOffset] AND (1 shl CurrBit) <> 0then result := 1;
  inc(CurrBit);
end;

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure SetBit;
begin
 PalIdx := (PalIdx AND 1);
end;

procedure ClrBit;
begin
 PalIdx := (PalIdx or 1);
end;


Etwas tiefer im Code

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
PalIdx := 0;

for i := 1 to 7 do
 begin
  if GetBit = 1 then
   SetBit
  else
   ClrBit;
  b := b shl 1;
 end;


Ich hoffe, der Code ist verständlich..

:r:

PS: das mit dem steinigen passiert wahrscheinlich auch in irgendwelchen nah-/fernöstlichen Foren ;)

_________________
if you have what they want - they'll find a way to take it (bruce sterling)
WOW - 10 JAHRE Mitglied beim Delphi-Forum. Wie die Zeit vergeht, Freunde.
Flamefire
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1207
Erhaltene Danke: 31

Win 10
Delphi 2009 Pro, C++ (Visual Studio)
BeitragVerfasst: Do 09.12.10 18:44 
Ok... dann guck dir mal genau an, was and und or macht:
00110101 AND
00000001 =
00000001

Ist das das, was du wolltest?

Außerdem hantierst du mit vielen globalen variablen rum --> Nicht gut.
Und letztendlich: Du setzt palIDX aber shiftest b (was auch immer b ist)
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5284
Erhaltene Danke: 27

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Do 09.12.10 18:48 
ungetestet:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
var
  bytes: array of byte;
  i: integer;
  r: byte;
begin
  r := 0;
  for i := 0 to high(bytes) do 
    r := r or (bytes[i] shl i);
  result := r;
end;


und wenn man ganz auf Nummer sicher gehen will:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
var
  bytes: array of byte;
  i: integer;
  r: byte;
begin
  r := 0;
  for i := 0 to high(bytes) do 
    r := r or ((bytes[i] shl i) and (1 shl i));
  result := r;
end;


btw: SetBit muss (or 1) und ClearBit (and 0) sein.
rizla Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 417
Erhaltene Danke: 2

XP
FPC mit Lazarus
BeitragVerfasst: Do 09.12.10 19:29 
@FlameFire:
ich meinte natürlich PalIdx := (PalIdx shl 1).

Danke jedenfalls. Wird schon funktionieren ;)

:r:

_________________
if you have what they want - they'll find a way to take it (bruce sterling)
WOW - 10 JAHRE Mitglied beim Delphi-Forum. Wie die Zeit vergeht, Freunde.