How to toggle a bit at specific position in Pascal

1 Answer

0 votes
program BitToggle;

function GetBits(n: LongInt): string;
var
  i: Integer;
begin
  GetBits := '';
  for i := 31 downto 0 do
    GetBits := GetBits + Chr(Ord('0') + ((n shr i) and 1));

  // remove leading zeros
  while (Length(GetBits) > 1) and (GetBits[1] = '0') do
    Delete(GetBits, 1, 1);
end;

var
  n: LongInt;
  pos: Integer;

begin
  n := 365;
  pos := 2;

  Writeln(GetBits(n));

  n := n xor (1 shl pos);

  Writeln(GetBits(n));
end.



{
run:

101101101
101101001
  
}


answered Apr 3 by avibootz
...