how can i make a "type" of size of only 2 BITS ...plz help me - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

how can i make a "type" of size of only 2 BITS ...plz help me

muaz_farooqmuaz_farooq Posts: 4Member

hi everyone,

i would be really greatefull if anybody can solve these 3 pproblems of mine :

1. I want to make a type in pascal whose size should be only 2 bits, so that there will be only 4 possible values for it(00,01,10,11).

2. Next i want to make an array of this type.

3. The important thing is i want to set the size of the array at runtime.

waiting eagerly for replies..thanking in anticipation

Comments

  • GaashiusGaashius Posts: 349Member
    :
    : hi everyone,
    :
    : i would be really greatefull if anybody can solve these 3 pproblems of mine :
    :
    : 1. I want to make a type in pascal whose size should be only 2 bits, so that there will be only 4 possible values for it(00,01,10,11).
    :
    : 2. Next i want to make an array of this type.
    :
    : 3. The important thing is i want to set the size of the array at runtime.
    :
    : waiting eagerly for replies..thanking in anticipation
    :
    1) Well probably you can't make a type which consists of 2 bits only. Here is another solution:
    [code]
    type
    My2bit = (b00,b01,b10,b11);
    [/code]

    2) Simple:
    [code]
    var
    bitarray=array[1..40] of my2bit; { 1 and 40 can be any other value }
    [/code]

    3) Well, at runtime - this is not possible I'm afraid. But, maybe I miunderstood your question, maybe this is what you're looking for:
    [code]
    var
    x:byte;
    bitarray:array[1..x] of my2bit;
    [/code]

    Hope this help you out. And, sorry for my english.

    ****************
    Any questions? Just ask!

    :-) [b][blue]GAASHIUS[/blue][/b] :-)


  • zibadianzibadian Posts: 6,349Member
    : :
    : : hi everyone,
    : :
    : : i would be really greatefull if anybody can solve these 3 pproblems of mine :
    : :
    : : 1. I want to make a type in pascal whose size should be only 2 bits, so that there will be only 4 possible values for it(00,01,10,11).
    : :
    : : 2. Next i want to make an array of this type.
    : :
    : : 3. The important thing is i want to set the size of the array at runtime.
    : :
    : : waiting eagerly for replies..thanking in anticipation
    : :
    : 1) Well probably you can't make a type which consists of 2 bits only. Here is another solution:
    : [code]
    : type
    : My2bit = (b00,b01,b10,b11);
    : [/code]
    :
    : 2) Simple:
    : [code]
    : var
    : bitarray=array[1..40] of my2bit; { 1 and 40 can be any other value }
    : [/code]
    :
    : 3) Well, at runtime - this is not possible I'm afraid. But, maybe I miunderstood your question, maybe this is what you're looking for:
    : [code]
    : var
    : x:byte;
    : bitarray:array[1..x] of my2bit;
    : [/code]
    :
    : Hope this help you out. And, sorry for my english.
    :
    : ****************
    : Any questions? Just ask!
    :
    : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    :
    :
    :
    The only 2 ways of creating dynamically memory lists are:
    - TMemoryStream (or a descendant of it)
    - linked-list
    A linked list uses records, where each record consists of at least 2 fields: 1 or more for the data, and 1 for the location of the next record. In your case that would be:
    [code]
    type
    PMy2Bits = ^TMy2Bits;
    TMy2Bits = record
    Bits: My2bit;
    Next: PMy2Bits;
    end;
    [/code]
    This way you can link records together, by only remembering the location of the first record:
    [code]
    var
    MyBitsArray: PMy2Bits;
    [/code]
    The drawback of this way is that the coding of the navigation quite involved is. For example: to get the indexth 2-bits you need something like this:
    [code]
    function GetBits(index: integer): My2bit;
    var
    i: integer;
    begin
    Current := MyBitsArray;
    i := 0;
    while i < Index do
    begin
    Current := Current^.Next;
    inc(i);
    end;
    Result := Current^.Bits;
    end;
    [/code]
    Also each record must be specifically be created using the New() statement.
  • dolev9dolev9 Posts: 147Member
    could be that althought it uses 2 bits it takes a whole byte for this.
    i checked it on boolean and i think i taken 1 byte for a every part of the array.
    check this out
    array[1..x]of boolean


    you can check their size using sizeof function.

    type
    My2bit = (b00,b01);
    bitarray=array[1..40] of my2bit;
    boolarray=array[1..40]of boolean;

    begin
    writeln(sizeof(bitarray));
    writeln(sizeof(boolarray));

    {hopefully each 1 will take 5 bytes and not 40...}

    good luck
    dolev

  • zibadianzibadian Posts: 6,349Member
    : could be that althought it uses 2 bits it takes a whole byte for this.
    : i checked it on boolean and i think i taken 1 byte for a every part of the array.
    : check this out
    : array[1..x]of boolean
    :
    :
    : you can check their size using sizeof function.
    :
    : type
    : My2bit = (b00,b01);
    : bitarray=array[1..40] of my2bit;
    : boolarray=array[1..40]of boolean;
    :
    : begin
    : writeln(sizeof(bitarray));
    : writeln(sizeof(boolarray));
    :
    : {hopefully each 1 will take 5 bytes and not 40...}
    :
    : good luck
    : dolev
    :
    :
    Each array will take 40 bytes, because the smallest memory unit pascal can handle is the byte. If you truely want to have just 2 bits, then you need to cast the array like this:
    [code]
    My2Bits: array[1..10] of byte;
    [/code]
    and use the various bitwise logical operators to get the bits. For example the following code will get the first 2 bits of the first element:
    [code]
    First2Bits := My2Bits[1] and $03;
    [/code]
  • Phat NatPhat Nat Posts: 757Member
    The other possiblilty (if you are being space conscious) would be to have a record that contains an array of 10 bytes & a pointer and then use functions to simplify searching.

    [code]
    TYPE
    MyBits = record
    Bits : Array[0..9] Of Byte;
    Next : Pointer;
    End;
    MyBitsPtr = ^MyBits;

    VAR
    Start : Pointer;
    Current : Pointer;
    Last : Pointer;

    PROCEDURE GetNewRecord;
    VAR
    NewRec : Pointer;
    Begin
    NewRec := New(MyBitsPtr);
    If Start = nil then
    Begin
    Start := NewRec;
    Current := Start;
    Last := Start;
    End
    ELSE
    Begin
    Last^.Next := NewRec
    Last := Last^.Next;
    End;
    Last^.Next := Nil;
    End;

    PROCEDURE DisposeRecords;
    Begin
    Current := Start;
    While Current^.Next <> Nil Do
    Begin
    Current := Current^.Next;
    Dispose(Start);
    Start := Current;
    End;
    If Start <> nil Then Dispose(Start);
    Start := nil;
    Current := nil;
    Last := nil;
    End;

    FUNCTION Get2Bit(Num : Word);
    Begin
    {Blah blah blah}
    End;

    FUNCTION GetNext2Bit;
    Begin
    {Blah blah blah}
    End;

    FUNCTION SetNext2Bit(Bits : Byte); { Or SetNext2bit(Bit1,Bit2 : Boolean);
    Begin
    {Blah blah blah}
    End;

    Begin
    GetNewRecord;
    DisposeRecords;
    End.
    [/code]

    This would allow eighty 2-bits variables for each record and also allow for expansion at the cost of only 14 bytes of memory for each.
    You could also expanded this to hold more in one record since the pointer is currently taking up almost 1/3 of the allocated memory.

    Phat Nat
  • netgertnetgert Posts: 331Member
    :
    : hi everyone,
    :
    : i would be really greatefull if anybody can solve these 3 pproblems of mine :
    :
    : 1. I want to make a type in pascal whose size should be only 2 bits, so that there will be only 4 possible values for it(00,01,10,11).
    :
    : 2. Next i want to make an array of this type.
    :
    : 3. The important thing is i want to set the size of the array at runtime.
    :
    : waiting eagerly for replies..thanking in anticipation
    :
    you can't use only 2 bits, however if you use 2 bits you can pack 4 of those values into one byte
    the bytes needed for n values of size 2 bits is n*2/8 rounded towards positive infinity (aka Ceil()) or this code:
    [code]
    size := (n*2) div 8;
    if n mod 4 <> 0 then size := size + 1; {not full filling of bytes}
    [/code]
    and to access the m'th byte you need
    [code]
    ofs := m mod 4;
    val := arr^[m div 4] shr ofs; {i'll explain arr^ later}
    arr^[m div 4] := (arr^[m div 4] and (not (3 shl ofs))) or (val shl ofs);
    {3 shr ofs will move 11B to the right place. not will make it into 11001111B etc. "and" will remove old data at that place and "or" will add new data}
    [/code]
    ofs is the local offset in the byte. Note: i write this as it comes up in my mind, might be typos or calculating errors
    to create dynamic array:
    [code]
    type
    parr = ^tarr
    tarr = array[0..0] of byte;
    ...
    var
    n: integer;
    arr: parr;
    ...
    n := 10; //we want to store 10 values
    size := (n*2) div 8;
    if n mod 4 <> 0 then size := size + 1; {not full filling of bytes}
    getmem(arr, size);
    fillchhar(arr^, size, 0);
    ...
    {and don't forget:} freemem(arr, size);
    [/code]
    arr will be a pointer to an array[0..0]. this might seem odd but if you turn of run-time range checking, you can access any element, even larger than 0 making this a dynamic array. note that you need to use variable indexes (not constants!) or borland's pascal compiler will die with an out-of-range compile-error if you use other than 0 for the index
    once you get the value
    [hr][red][italic][b]N[/b][/red][blue]et[/blue][red][b]G[/b][/red][blue]ert[/italic][/blue][hr]

  • anonedsanoneds Posts: 15Member
    : : could be that althought it uses 2 bits it takes a whole byte for this.
    : : i checked it on boolean and i think i taken 1 byte for a every part of the array.
    : : check this out
    : : array[1..x]of boolean
    : :
    : :
    : : you can check their size using sizeof function.
    : :
    : : type
    : : My2bit = (b00,b01);
    : : bitarray=array[1..40] of my2bit;
    : : boolarray=array[1..40]of boolean;
    : :
    : : begin
    : : writeln(sizeof(bitarray));
    : : writeln(sizeof(boolarray));
    : :
    : : {hopefully each 1 will take 5 bytes and not 40...}
    : :
    : : good luck
    : : dolev
    : :
    : :
    : Each array will take 40 bytes, because the smallest memory unit pascal can handle is the byte. If you truely want to have just 2 bits, then you need to cast the array like this:
    : [code]
    : My2Bits: array[1..10] of byte;
    : [/code]
    : and use the various bitwise logical operators to get the bits. For example the following code will get the first 2 bits of the first element:
    : [code]
    : First2Bits := My2Bits[1] and $03;
    : [/code]
    :


    This is true of Borland variants, but is this a Borland Only message board?

    The following results in usage of only two bits on my compiler for each element (UCSD IV.0). Your mileage may vary. You need to consult your compiler docs or use sizeof to determine the usage:

    [code]

    Var My2Bits: packed array[0..1] of 0..3;

    [/code]

    My2Bits occupies only one 16-bit word of memory, and 12 of the bits in that word are unused.

    Ben
  • netgertnetgert Posts: 331Member

    : This is true of Borland variants, but is this a Borland Only message board?
    [blue]Borland's compiler is the default and usually preferred one. however, nowadays freepascal is taking over. you can't assume someone is using XYZ compiler, however it is safe to assume BPC to be used as all pascal compilers need to support what it does[/blue]
    :
    : The following results in usage of only two bits on my compiler for each element (UCSD IV.0). Your mileage may vary. You need to consult your compiler docs or use sizeof to determine the usage:
    :
    : [code]
    :
    : Var My2Bits: packed array[0..1] of 0..3;
    :
    : [/code]
    :
    : My2Bits occupies only one 16-bit word of memory, and 12 of the bits in that word are unused.
    :
    : Ben
    :

    [hr][red][italic][b]N[/b][/red][blue]et[/blue][red][b]G[/b][/red][blue]ert[/italic][/blue][hr]

Sign In or Register to comment.