Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

read each bit of binary code?

gerardmcmanusgerardmcmanus Member Posts: 13
Ok, I know you can do :=

while decimal <> 0 do
begin
count := count + 1;
remain[count] := decimal mod 2;
decimal := decimal div 2;
end;

... to get the binary code of an integer, but I would like to know how I could read each bit of the binary code that makes up the integer.

I can set the pointer, and then read the value once I de-reference but want to read each bit individually. I though that mem[] could be used but I am not too sure about it and havent got anywhere.

at the moment I am at :=

{set dec}
dec := 12;

{set pointer}
p := @dec;

{find each value}
for c:=0 to 7 do
Begin
if mem[p^:c] > 0 Then
Begin
binArr[c] := 1;
writeln(c,' : ',mem[p^:c]);
End
Else
Begin
binArr[c] := 0;
writeln(c,' : ',mem[p^:c]);
End;
End;

for c:= 0 to 7 do
write(binArr[c]);
readln;

output :

[img=http://gerardmcmanus.homeip.net/TurboPascal/scrShts/DecToBinV2otpt.JPG]

Thanks

Gerard



Comments

  • AtexAtex Member Posts: 268
    This would convert a byte to its binary format, expand it to support different types
    [code][color=Blue]

    const po2:array[0..7] of byte=(1,2,4,8,16,32,64,128); {power of 2}

    function byte2binary(b:byte):string;
    var s:string[8];
    i:byte;
    begin
    s:='';
    for i:=7 downto 0 do
    if (b and po2[i]=po2[i]) then s:=s+'1'
    else s:=s+'0';
    byte2binary:=s;
    end;
    [/color][/code]
  • gerardmcmanusgerardmcmanus Member Posts: 13
    nice solution, thats much more understandable than going down the road of mod 2, but I am only using an int to Byte Prog as an example of where I might want to read a single bit, and am more interested in getting down to each bit and finding if its a 1 or zero, true or false.

    I know you can find the value of an address, but how can I read each bit that makes up that value?
  • AtexAtex Member Posts: 268
    [code][color=Blue]
    const po2:array[0..7] of byte=(1,2,4,8,16,32,64,128);

    function get_bit(b,bit_no:byte):boolean;
    begin
    get_bit:=(b and po2[bit_no]=po2[bit_no]);
    end;

    {Turns a given bit to "1"}
    function set_bit(b,bit_no:byte):byte;
    begin
    set_bit:=b or po2[bit_no];
    end;

    {Clears a given bit, returns b if bit is already "0"}
    function clear_bit(b,bit_no:byte):byte;
    begin
    if get_bit(b,bit_no) then clear_bit:=b xor po2[bit_no]
    else clear_bit:=b;
    end;


    [/color][/code]

  • AtexAtex Member Posts: 268
    To handle signed integer types ( shortint, integer, longint ) try:
    [code][color=Blue]
    var t:integer;
    b:array[0..1] of byte absolute t;
    {another variable referring to the same address}

    l:longint;
    b2:array[0..3] of byte absolute l;

    i:shortint;
    b3:byte absolute i;

    {Changing t,l, or i will change b,b2 and b3. And vice versa}
    [/color][/code]
    Don't forget data get's stored by the little-endian way.
    i.e. b[1],b[0] not b[0],b[1]

  • ActorActor Member Posts: 447 ✭✭
    : ... am more interested
    : in getting down to each bit and finding if its a 1 or zero, true or
    : false.
    :
    Pass this function an integer and a position. It will return TRUE if and only if the bit at that position is 1. The least significant bit is bit zero. Similar code will work for types Byte, Word and LongInt.
    [code]
    Function Bit (i : Integer ; j : Byte) : Boolean ;
    {
    returns TRUE if bit at position j is 1
    }
    CONST
    MASK : Array [0 .. 15] of Word
    = ( 1, 2, 4, 8, 16, 32, 64, 128,
    256, 512, 1024, 2048, 4096, 8192, 16384, 32768) ;

    begin
    Bit := (i AND MASK[j]) > 0
    end ;
    [/code]

Sign In or Register to comment.