AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

bisschen Code zum Thema Bitmask

Ein Thema von heri · begonnen am 30. Apr 2008
Antwort Antwort
heri

Registriert seit: 20. Mär 2006
81 Beiträge
 
Delphi 2006 Enterprise
 
#1

bisschen Code zum Thema Bitmask

  Alt 30. Apr 2008, 15:29
ich habe mich heute ein bisschen mit dem Thema Bitmasken umhergeschlagen und dazu folgenden URL gefunden

http://www.nitrogen.za.org/viewtutorial.asp?id=17
Zitat:
This example will use bytes as the basis of our binary flags, but the concept is the same for any number of bits.
Imagine an ordering system in a pizza restaurant. Each pizza can have any combination of eight toppings:
Cheese, Tomato, Ham, Pineapple, Mushrooms, Pepperoni, Chicken and Chilli.
Each pizza's combination of these toppings can be expressed as a single byte value.
und daraus den Code ein bisschen zum Testen komplettiert:

Delphi-Quellcode:
//Sets a flag according to the bitmask.
Procedure SetFlag(var Flags: byte; Bitmask: byte);
begin
  Flags := Flags OR Bitmask;
end;

//Clears a flag according to the bitmask.
Procedure ClearFlag(var Flags: byte; Bitmask: byte);
begin
  Flags := Flags AND (NOT Bitmask);
end;


//Now we define each topping as a bitmask.
//Normally you will define your bitmasks as constants because they never change:
procedure TForm1.Button1Click(Sender: TObject);
var
Temp: byte;
Toppings: byte; //This will be our flag container variable.
IsVegitarian, HasMushrooms: Boolean;
begin

   //Now we can perform operations on our pizza toppings:

   //To create a basic pizza:
   Toppings := topCheese OR topTomato;

   //To add Ham, Cheese and Tomato to a pizza:
   SetFlag( Toppings, topCheese or topTomato );

   SetFlag( Toppings, topHam );
   SetFlag( Toppings, topPineapple );
   SetFlag( Toppings, topMushrooms );

   //To remove chilli from a pizza:
   ClearFlag( Toppings, topChilli ); //Note: This will do nothing if Toppings didnt contain topChilli in the first place.

   ClearFlag( Toppings, topHam );

   //To check for a vegitarian pizza, you could write the following statement:
   IsVegitarian := (Toppings AND (topHam or topChicken)) = 0; //Checking that both the topHam and topChicken bits are set to 0.
   if IsVegitarian then
      showmessage('IsVegitarian');

   //To check if a pizza has mushrooms:
   HasMushrooms := (Toppings AND topMushrooms) = topMushrooms; // > 0; //Checking that the topMushrooms bit is set.
   if HasMushrooms then
      showmessage('HasMushrooms');

   //Now lets try something slightly more advanced. Does a pizza have Ham or Pineapple or Both?
   Temp := ( Toppings AND (topHam OR topPineapple) );

   if Temp = topHam then
      showmessage('Just Ham')
   else
   if Temp = topPineapple then
      showmessage('Just Pineapple')
   else
   if Temp = (topHam or topPineapple) then
      showmessage( 'Both Ham and Pineapple ')
   else
      showmessage( 'neither Ham nor Pineapple ')

end;

und weils gleich noch zum Thema passt etwas Code hier aus der DP
Delphi-Quellcode:
function GetBitMask(const AByte:byte):TBitmask;
var
  i: integer;
begin

  for i := 0 to 7 do
    result.Bits[i] := (AByte SHR i AND $1) = $1;

end;

function BitmaskToByte(AValue:TBitmask):byte;
var
i:integer;
begin
result := 0; // Rückgabewert auf 0 setzten. WICHTIG. Weglassen dieser Zeile kann zu faulen Ergebnisen führen.

  for i := 0 to 7 do
   if avalue.bits[i] then //Wenn dieser Wert der Bitmaske auf "1" steht, muss das Ergebnis um
      Inc( Result, 1 SHL i ); //2 hoch der Position in der Bitmaske incrementiert werden.

end;
dies als kleiner Beitrag falls sonst mal jemand mit Bitmasken arbeitet
(oder ich selber es vergesse und beim nächsten mal suchen in dp dies hier wieder finde :wink: )

:gruebel:
und falls jemand noch die Umkehrfunktion zum BitmaskToByte hat - also ByteToBitmask - dann wäre ich sehr froh über diesen Nachtrag!

thx
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 18:10 Uhr.
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