Author Topic: binary  (Read 596 times)

0 Members and 1 Guest are viewing this topic.

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
binary
« on: September 23, 2008, 06:23:30 pm »
I was wondering if there was a thing in Soldat pascal that put a binary number into an int of some sort. Similar to doing $ prior a hex number. (so i can put it in constants)

Side question: Is it possible to get the value of a given set of bits... for example, the first 8 bits (far right 8 ) found inside of lets say an integer or whatever? (not so important) (like 1000000100100010 (33058) would be 00100010 (34))
« Last Edit: September 23, 2008, 07:14:40 pm by DorkeyDear »

Offline iDante

  • Veteran
  • *****
  • Posts: 1967
Re: binary
« Reply #1 on: September 23, 2008, 07:37:53 pm »
http://www.hu.freepascal.org/lists/fpc-pascal/2004-April/006912.html
Dunno if that actually works... untested. (%)

Question 2:
http://forums.soldat.pl/index.php?topic=28689.0
to get the far right byte just do
Code: [Select]
var
    foo: string;
    bar: integer;
begin
    foo := toString(33058,2); // '1000000100100010'
    bar := parseInt(copy(foo,length(foo)-8,length(foo)),2); // 34
end;
or something like that.
« Last Edit: September 23, 2008, 07:55:48 pm by iDante »

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: binary
« Reply #2 on: September 23, 2008, 07:55:56 pm »
for others to see
http://snippets.dzone.com/posts/show/2194
i found:
Code: [Select]
function GetBit(const Value: DWord; const Bit: Byte): Boolean;
begin
  Result := (Value and (1 shl Bit)) <> 0;
end;

function ClearBit(const Value: DWord; const Bit: Byte): DWord;
begin
  Result := Value and not (1 shl Bit);
end;

function SetBit(const Value: DWord; const Bit: Byte): DWord;
begin
Result := Value or (1 shl Bit);
end;

function EnableBit(const Value: DWord; const Bit: Byte; const TurnOn: Boolean): DWord;
begin
Result := (Value or (1 shl Bit)) xor (Integer(not TurnOn) shl Bit);
end;
untested yet