![]() | |
| | #1 (permalink) |
| Üye Üyelik Tarihi: 07/2006
Mesaj: 18
|
Merhaba arkadaşalar, Delphi de var data:byte; şeklinde bir değişken tanımladım ve bunun herhangibir bitine erişip o bitin üzerinde and ,or işlemleri yaptırmam lazım bunu delphide nasıl halledebilirim. notasyon olarak doğru olmasa da zoruyu anlatmak için şöyle bir örnek vereyim data.0:=data.1 and data.0; bu şekilde bir işlemi delphide nasıl yaparım? |
| | |
| | #4 (permalink) |
| Üye Üyelik Tarihi: 01/2007 Yer: Ceviz Wiki(ydi)
Mesaj: 57
|
Şu bağlantılara bir bakın bakalım işinize yarayan birşey var mı: Delphi Basics: Files, BlockRead, BlockWrite... Pascal'da 3 tip dosya yapısı vardır Delphide Dosyalara BYTE eklemek Dizilere Doğrudan Atama Write your own 24 bit BMP Delphi Bit/Byte-Moving Bit manipulation Bit-wise manipulation |
| | |
| | #7 (permalink) |
| Üye Üyelik Tarihi: 07/2006
Mesaj: 18
|
Arkadaşlar buyrun çok kapsamlı bir kod parçası içinde bir çok fonksiyon var unit FlagsMgr; interface uses Classes, Math, SysUtils, Dialogs; type TFlagType = cardinal; // This can be changed to any integer type. // If signed - don't mess with the sign bit by using any // negative values or using that highest bit position. {These routines work on positive values for integer types. If unsigned, then all bits are available = SizeOf(TFlagType)*8 If signed then SizeOf(TFlagType)*8-1 are available.} {Using "not" on signed integers flips the sign bit, not all bits as it does on unsigned integers. Because these routines work on signed integers as well, the "not" operator isn't included.} function GetFlagValue(Expn: byte): TFlagType; function GetFlagExpon(Flag: TFlagType; var Expn: ShortInt): boolean; procedure InsertFlag (var Flags: TFlagType; Flag: TFlagType); function ContainsFlag (Flags, Flag: TFlagType): boolean; function ContainsCommonFlags (Flags1, Flags2: TFlagType): boolean; function CommonFlags (Flags1, Flags2: TFlagType): TFlagType; procedure RemoveFlag (var Flags: TFlagType; Flag: TFlagType); procedure RemoveCommonFlags( var Flags1, Flags2: TFlagType; FromWhich: integer); procedure RemoveUniqueFlags( var Flags1, Flags2: TFlagType; FromWhich: integer); function IntToBinary(AnInt: TFlagType; Commas: boolean): string; function NextPowerOfTwo (Flag: TFlagType): TFlagType; function IsPowerOfTwo (Flag: TFlagType): boolean; function PosHighestOnBit(Flags: TFlagType): integer; function DecToBase(DecNo: integer; Base: word): string; function BaseToDec(BaseNo: string; Base: word): integer; implementation {******************************* Flag Manipulation ***************************} {Flags, as it is used here is a number that represents a series of yes / no, true / false conditions - one for each bit. The bit is either on or off. A flag to add to flags item it a number that has a decimal value of a power of two. That is, it's positive 1, 2, 4, 8, 16, 32, 64, 128, 256... An easy way to implement this to use the integer values [0..(SizeOf[TFlagType]*8-1)] as exponents powering 2, that way for int64 you're dealing with 0..63 rather than Example: InsertFlag (MyFlags, GetFlagValue(5)); Sets the bit which represent 2^5 (0010 0000) to true or "1" in MyFlags Doesn't effect other bits which may already be set.} function GetFlagValue(Expn: byte): TFlagType; // could be called - PowerTwo(Expn: byte): TFlagType; // returns an integer value = to 2^Expn //var i: TFlagType; begin Result := TFlagType(1) shl Expn; { Also could be performed: Result := round(power (2, Expn));} end; function GetFlagExpon(Flag: TFlagType; var Expn: ShortInt): boolean; // returns true if the Flag has only one bit set on, and rest are off // that is, the value of Flag is an integer power of two // and returns that exponent value in Expn. If Flag has more than one // bit set to true, return value of function is false var i, MaxEx: byte; TryFlag: TFlagType; begin Result := false; Expn := -1; if Flag > 0 then begin MaxEx := SizeOf(TFlagType)*8; if Low(TFlagType) < 0 then dec(MaxEx); // TFlagType is currently cardinal // if I change flag type to a different type // this code may be necessary for i := 0 to MaxEx do begin TryFlag := GetFlagValue(i); if ContainsFlag(Flag, TryFlag) then begin RemoveFlag(Flag, TryFlag); if Flag = 0 then begin Expn := i; Result := true; end; Break; end; end; end; end; procedure InsertFlag (var Flags: TFlagType; Flag: TFlagType); begin Flags := Flags or Flag; // bitwise or turns on the flag if it exists or not end; function ContainsFlag (Flags, Flag: TFlagType): boolean; begin Result := (Flags and Flag) = Flag; end; function ContainsCommonFlags ( Flags1, Flags2: TFlagType): boolean; begin Result := ((Flags1 and Flags2) > 0); end; procedure RemoveFlag (var Flags: TFlagType; Flag: TFlagType); // Flag can contain one or many of Flags values - they will all be removed begin Flag := not Flag; Flags := Flags and Flag; end; function CommonFlags ( Flags1, Flags2: TFlagType): TFlagType; // Result = a value with all the flags that are common to Flags1 & Flags2. begin Result := Flags1 and Flags2; end; procedure RemoveCommonFlags( var Flags1, Flags2: TFlagType; FromWhich: integer); // FromWhich indicates which flags value you want to remove the common flags // from. Possible values are 1: Remove from Flags1; 2: Remove from Flags2; // and 3: Remove from both; var Com: TFlagType; begin Com := CommonFlags (Flags1, Flags2); if (FromWhich = 1) or (FromWhich = 3) then RemoveFlag(Flags1, Com); if (FromWhich = 2) or (FromWhich = 3) then RemoveFlag(Flags2, Com); end; procedure RemoveUniqueFlags( var Flags1, Flags2: TFlagType; FromWhich: integer); // FromWhich indicates which flags value you want to remove the unique flags // from. Possible values are 1: Remove from Flags1; 2: Remove from Flags2; // and 3: Remove from both; var Uniq: TFlagType; begin Uniq := Flags1 xor Flags2; if (FromWhich = 1) or (FromWhich = 3) then RemoveCommonFlags(Flags1, Uniq, 1); if (FromWhich = 2) or (FromWhich = 3) then RemoveCommonFlags(Uniq, Flags2, 2); end; function PosHighestOnBit(Flags: TFlagType): integer; // returns the position ofthe last "On" bit in AFlags counting from low bit to high bit // var i: byte; MaxI: byte; Flag: TFlagType; begin Result := 0; i := 0; MaxI := SizeOf(TFlagType)*8 - 1; repeat Flag := GetFlagValue(i); if ContainsFlag(Flags, Flag) then Result := i + 1; inc(i); until (Flag > Flags) or (i = MaxI); end; function IntToBinary(AnInt: TFlagType; Commas: boolean): string; // Visualize MyBits [0..IntSize-1] where 0 is the leftmost bit var MyBits: TBits; n: byte; // the exponent RemInt: TFlagType; Cntr, Offset: byte; IntSize: byte; begin IntSize := SizeOf(TFlagType)*8; MyBits := TBits.create; MyBits.size := IntSize; RemInt := AnInt; if RemInt < 0 then begin // condition check may be required if TFlagType <> cardinal MyBits.Bits[IntSize-1] := true; RemInt := RemInt*(-1)-1;// change sign and shift value by one end; if Low(TFlagType) < 0 then Offset := 2 // condition check may be required if TFlagType <> cardinal else Offset := 1; for n := (MyBits.Size - Offset) downto 0 do begin // 2^n represents the values held in each place from the highest down to the lowest // check a value against 2^n, if the value is greater than 2^n, then it contains it // and set the bit to on. // if RemInt >= round(Power(2, n)) then begin if RemInt >= GetFlagValue(n) then begin MyBits.Bits[n] := true; // RemInt := RemInt - round(Power(2,n)); RemInt := RemInt - GetFlagValue(n); end else begin MyBits.Bits[n] := false; end; end; // now convert to string Result := ''; Cntr := 0; for n := (MyBits.Size - 1) downto 0 do begin if MyBits.Bits[n] then Result := Result + '1' else Result := Result + '0'; inc(Cntr); if (Cntr <> IntSize) and ((Cntr mod 4) = 0) then if Commas then Result := Result + ','; end; MyBits.free; end; function IsPowerOfTwo (Flag: TFlagType): boolean; // Returns true if the value of Flag is a power of two for an exponent in the // range of 0 to MaxExp. A Flag's valid range is 1..2^62 // This allows a FlagContainer to have up to 63 possible flags var exp : word; MaxExp : integer; begin MaxExp := SizeOf(TFlagType)-1; Result := false; for exp := 0 to MaxExp do begin if Flag = round(Power(2, exp)) then begin Result := true; break; end; if Flag < round(Power(2, exp)) then break; end; end; function NextPowerOfTwo (Flag: TFlagType): TFlagType; // returns the next power of two higher than the current value for flag // or zero if the exponent > MaxExp (62). var exp: Word; MaxExp : integer; begin MaxExp := SizeOf(TFlagType)-1; exp := 0; while (exp <= SizeOf(TFlagType)-1) and (round(Power(2, exp)) < Flag) do inc(exp); if exp > MaxExp then Result := 0 else begin if Flag < (round(Power(2, exp))) then Result := round(Power(2, exp)) else Result := round(Power(2, exp+1)); end; end; function DecToBase(DecNo: integer; Base: word): string; { converts any decimal integer to any base in the range of [2..16] and returns the converted number as a string. Example of logic implemented for base five Decimal 46 = 141 in base 5 First iteration of while loop 46/5 = 9.2, Drop 9 & multiply 0.2 * 5 = 1 -> right most digit Next iteration of while loop, process dropped 9 9/5 = 1.8, Drop 1 & multiply 0.8 * 5 = 4 -> next left digit Next iteration of while loop, process dropped 1 1/5 = 0.2, Drop 0 & multiply 0.2 * 5 = 1 -> left most digit Rem is zero - end processing. } var Rem: integer; NextDig: integer; ADig: string; begin Result := ''; if (Base >= 2) and (Base <= 16) then begin if DecNo = 0 then Result := '0' else begin Rem := Abs(DecNo); // get positive value while Rem <> 0 do begin NextDig := Round(frac(Rem/Base)*Base); case NextDig of 10: ADig := 'A'; 11: ADig := 'B'; 12: ADig := 'C'; 13: ADig := 'D'; 14: ADig := 'E'; 15: ADig := 'F'; else ADig := IntToStr(NextDig); end; // case Result := ADig + Result; Rem := Round(int(Rem/Base)); end; // while if DecNo < 0 then Result := '-' + Result; end; // else end else ShowMessage('Invalid base passed to function "DecToBase"'); end; function BaseToDec(BaseNo: string; Base: word): integer; { converts a value in any base represented by a string to a decimal integer where base is in the range of [2..16] and returns the converted number as an integer. Example of logic implemented for base five Base 5 # 141 = decimal 46 First iteration of while loop left most digit is 1 1 / 5 = 0.2; Add nothing from above = 0.2 0.2 * 5 = 1 Next iteration of while loop next digit to right is 4 4 / 5 = 0.8; Add 1 from above to 0.8 = 1.8 1.8 * 5 = 9 Next iteration of while loop next digit to right is 1 1 / 5 = 0.2; Add 9 from above to 0.2 = 9.2 9.2 * 5 = 46 No more digits to process.} var Rem: string; ChStr: string; ADig: integer; begin Result := 0; if (Base >= 2) and (Base <= 16) then begin if BaseNo <> '0' then begin // get absolute value if BaseNo[1] = '-' then Rem := copy(BaseNo, 2, length(BaseNo)) else Rem := BaseNo; while Rem <> '' do begin case Rem[1] of 'A', 'a': ADig := 10; 'B', 'b': ADig := 11; 'C', 'c': ADig := 12; 'D', 'd': ADig := 13; 'E', 'e': ADig := 14; 'F', 'f': ADig := 15; else ADig := StrToInt('' + Rem[1]); end; Result := Round((Result + (ADig/Base)) * Base); Rem := copy(Rem, 2, length(Rem)); end; // while if BaseNo[1] = '-' then Result := (-1) * Result; end; // else end else ShowMessage('Invalid base passed to function "BaseToDec"'); end; end. ///////////////////////////////// Save as a unit to use - not a form. From Delphi menu: File|New In ensuing dialog, select Unit. Delphi will open a new unit. Under the unit's name: "unit Unit1;" Copy and paste my entire unit. Then delete my unit's name line: "unit FlagsMgr" Then do a file SaveAs "FlagsMgr" Save either in Delphi's Bin directory, or in a folder that is in your Application's Search Path. Then in your form's uses clause, add "FlagsMgr". All the functions declared in FlagsMgr's interface section are then available to your form. Arkadaşlar type TFlagType = cardinal; deki cardinal i herhangi bir integer türüne çevirip kullanın yoksa insertflag vs gibi fonksiyonlarda tür uyuşmazlığı hatası veriyor. Sorularınız olursa elimden geldiğimce yardımcı olmaya çalışırım. İyi çalışmalar. |
| | |
| | #8 (permalink) |
| Üye Üyelik Tarihi: 07/2006
Mesaj: 18
|
Bunlar da da açıklama ve bir de ilave fonksiyon var InsertFlag (Deg, GetFlagValue(0)); // set low bit on InsertFlag (Deg, GetFlagValue(1)); // set second bit from right on InsertFlag (Deg, GetFlagValue(2)); // set third bit from right on RemoveFlag (Deg, GetFlagValue(0)); // turn off low bit RemoveFlag (Deg, GetFlagValue(1)); // turn off second bit RemoveFlag (Deg, GetFlagValue(2)); // turn off third bit The value of Deg is stored & manipulated as an integer type. To view the binary value, you must use function IntToBinary which returns a string representation of the binary value. To view the binary value as a string in a TEdit: Edit1.text := IntToBinary(Deg, true); If you prefer a space rather than commas, you can change the 3rd to last line of IntToBinary where the commas are added to the result to insert a space instead, or write a similar function that adds spaces. ////////////////////////////////////////////////////////////////////////////////////// if you want to view your value as octal: Edit1.text := DecToBase(Deg, 8); or hex: Edit1.text := DecToBase(Deg, 16); I see that in the DecToBase I have DecNo declared as an integer instead of TFlagType. I don't think that will cause you trouble. Delphi provides a library function IntToHex which also returns a string. ////////////////////////////////////////////////////////////////////////////////////// If you want to change a binary string representation back to an integer value, you can do something like this. I haven't tried it. function BinaryToInt(sBi: string): integer; var i: integer; CleanSBi: string; begin CleanSBi := ''; // remove non binary characters from string for i := 1 to length(sBi) do if (sBi[i] = '0') or (sBi[i] = '1') then CleanSBi := CleanSBi + sBi[i]; Result := BaseToDec(CleanSBi, 2); end; with that, you should be able to display the value of your edit in a label procedure TForm1.Edit1Change(Sender: TObject); begin Label1.caption := IntToStr(BinaryToInt(Edit1.text)); end; |
| | |
![]() |
| Bookmarks |
| Seçenekler | |
| |
Benzer Konular | ||||
| Konu | Konuyu açana göre | Forum | Cevap | En Son Mesaj |
| Access ile dropdownlist erişimi | Illuminati | Visual Basic | 18 | 27/04/2008 02:46 |
| nete erişimi kısıtlamak | DotNetKid | Windows | 5 | 12/10/2006 10:10 |
| oku.php ye direk erişimi yasaklamak | Manyakgeyik | PHP | 8 | 25/12/2004 22:31 |
| uydu bağlantısıyla net erişimi | DotNetKid | Internet | 1 | 27/10/2004 18:59 |
| uydu aracılığı ile internet erişimi | MEHMET | Donanım | 2 | 11/06/2004 22:02 |
| Reklamlar & Desteklenenler | |
| Hassas Valf | Hassas Kaplama | Antalyamız | Gazete | Ticari Bilişim | Hakan Müştak | Rüya Tabirleri | Kadın | Hastalıklar | Cepte msn ve e-posta | Webmaster | Antalya Aupair | Turkish Property Antalya | Forum | Chat | Perde | Adsl | Araba | bolindir.com | guncelle.com | livescore | Web Tasarım | evden eve nakliyat | forum | evden eve | sohbet | Resimcim| Kalifiye İnsan Kaynakları | Web Tasarım | Oyun | Yusuf KOÇ | Akın Yorulmaz | şiir | UFO | Web Tasarım | Oyunlar | Canlı Tv | |