Autor Beitrag
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: Fr 03.10.08 15:52 
Wie kann ich aus einem Byte ein bestimmtes Bit testen?
Also eine Abfrage, die true ergibt, wenn das bit gesetzt ist

Also zb: dez "72"-->Bin 01001000
Jetzt will ich (in einer for schleife) bit x testen (bzw ebenfals bit 7-x)
Bei x=1 wäre das 1 und bei 7-x 0 in diesem Bsp

Ich hoffe das ist verständlich.

Wichtig wäre noch: Überlauf beachten
Also wenn x=9 ist dann soll es wieder das 1.Bit sein

Dachte an: (myByte And (1 shl (x mod 7)))=1

aber es gibt vl noch was schnelleres...außerdem scheint es nicht ganz zu stimmen
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Fr 03.10.08 16:00 
ausblenden Delphi-Quelltext
1:
(myByte And (1 shl (x mod 8)))>0					

1. Ein Byte hat 8 bit, also x mod 8
2. wenn du mit 1 shl x arbeitest, wird von hinten nach vorne nummeriert, und das erste Bit hat den index 0!
3. Du darfst natürlich nicht auf =1 prüfen, da dies nur bei x=0 der Fall wäre. Darüberhinaus wäre das Ergebnis 2, 4, 8 usw.

Bei deinem Beispiel
0 1 0 0 1 0 0 0
7 6 5 4 3 2 1 0

wäre also das Ergebnis für 3 und 6 true.

_________________
PROGRAMMER: A device for converting coffee into software.
Flamefire Threadstarter
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: Fr 03.10.08 16:45 
Ok dann vl mal den ganzen code

es soll ein array of Bit (ein monochromes bild als bytearray) in ein bytearray umgewandelt werden

mein aktueller code ist der hier:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
      SetLength(bild,height);
      for y:=0 to height-1 do begin
        SetLength(bild[y],width);
        for x:=0 to width-1 do begin
          bild[y][x]:=(source[(x + y * width) shr 3And (1 shl (x mod 8)))>0;
        end;
      end;


stimmt das so weit?

EDIT: ok habs getestet und geht

kommt man um das mod 8 noch drumrum?
weil ne mod ist ja doch etwas langsamer...nja wär ne schlimm
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Fr 03.10.08 17:10 
Hallo,

mod 8 == AND (8-1)

Gruß Horst
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Fr 03.10.08 17:16 
Alternativ:
ausblenden Delphi-Quelltext
1:
2:
if Zahl and (1 shl BitNummer) > 0 then
   ... {gesetzt}

Das mod kann man sich sparen.
€: Sofenr es sich um Byte-Typen handelt ist die Methode mit Mod vorzuziehen. Diese ist dann doch schneller. ;)
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Fr 03.10.08 17:55 
Hallo,
sind deine Daten in einem Bitarray?
Um schnell zu sein weniger komplierte Zugriffe. Du greifts 8-mal auf die selbe Stelle zu
:= (source[(x + y * width) shr 3]...
Mit Zeigern erspartst Du dem Computer die ständige Rechnerei der Indizes

lieber:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
type 
  pbyte = ^byte;
var
  tmp ; byte;
  pSource,
  pGoal  : pByte;
...

pSource := @source[0];// Zeiger auf das erste Element;

y := 0;
while y< height do
  begin
  SetLength(bild[y],width); 
  pGoal :=@bild[y][0];//Adresse wohin gespeichert wird
  x := 0;
  while x< width do
    begin
    tmp :=pSource^; // Element merken
    For i := 0 to 7 do
      begin
      pGoal^ := tmp AND 1;
      inc(pGoal);
      tmp := tmp shr 1// nacheinander erscheinen die Bit an Position 0   2^0 = 1
      end
    inc(x,8);
    end;
  inc(y);
  end;


Gruß Horst