Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
how can i make a "type" of size of only 2 BITS ...plz help me Posted by muaz_farooq on 29 Apr 2005 at 6:27 AM

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
Report
Re: how can i make a "type" of size of only 2 BITS ...plz help me Posted by Gaashius on 29 Apr 2005 at 7:43 AM
:
: 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:
type
 My2bit = (b00,b01,b10,b11);


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


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:
var
 x:byte;
 bitarray:array[1..x] of my2bit;


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

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

GAASHIUS


Report
Re: how can i make a "type" of size of only 2 BITS ...plz help me Posted by zibadian on 29 Apr 2005 at 8:46 AM
: :
: : 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:
:
: type
:  My2bit = (b00,b01,b10,b11);
: 

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

:
: 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:
:
: var
:  x:byte;
:  bitarray:array[1..x] of my2bit;
: 

:
: Hope this help you out. And, sorry for my english.
:
: ****************
: Any questions? Just ask!
:
: GAASHIUS
:
:
:
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:
type
  PMy2Bits = ^TMy2Bits;
  TMy2Bits = record
    Bits: My2bit;
    Next: PMy2Bits;
  end;

This way you can link records together, by only remembering the location of the first record:
var
  MyBitsArray: PMy2Bits;

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:
  
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;

Also each record must be specifically be created using the New() statement.
Report
Re: how can i make a "type" of size of only 2 BITS ...plz help me Posted by dolev9 on 29 Apr 2005 at 11:15 AM
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

Report
Re: how can i make a "type" of size of only 2 BITS ...plz help me Posted by zibadian on 29 Apr 2005 at 12:38 PM
: 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:
  My2Bits: array[1..10] of byte;

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:
  First2Bits := My2Bits[1] and $03;

Report
Re: how can i make a "type" of size of only 2 BITS ...plz help me Posted by Phat Nat on 30 Apr 2005 at 5:40 PM
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.

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.


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
Report
Re: how can i make a "type" of size of only 2 BITS ...plz help me Posted by anoneds on 2 May 2005 at 5:59 PM
: : 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:
:
:   My2Bits: array[1..10] of byte;
: 

: 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:
:
:   First2Bits := My2Bits[1] and $03;
: 

:


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:


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



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

Ben
Report
Re: how can i make a "type" of size of only 2 BITS ...plz help me Posted by netgert on 3 May 2005 at 11:48 AM

: This is true of Borland variants, but is this a Borland Only message board?
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
:
: 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:
:
:
: 
: Var My2Bits: packed array[0..1] of 0..3;
: 
: 

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


NetGert[/italic]


Report
Re: how can i make a "type" of size of only 2 BITS ...plz help me Posted by netgert on 1 May 2005 at 1:37 AM
:
: 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:
size := (n*2) div 8;
if n mod 4 <> 0 then size := size + 1; {not full filling of bytes}

and to access the m'th byte you need
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}

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:
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);

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

NetGert[/italic]





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.