01 filed1 pic s9(3) comp-3.
for this internally how many bytes will it take.do v have to use the formula n/2 + 1 or else n+1/2.if i'm using n/2 + 1 then it'll take 3 bytes. if i use the other formula it'll take 2 bytes.
so which one shuld be used.
Joined: 22 Nov 2005 Posts: 700 Location: Troy, Michigan USA
Let?s start out with some truths.
1 - The compiler always allocates comp-3 fields in full bytes (unless you?re using a Burroughs computer that allocates in 1/2 bytes, I?ll bet most of you don?t even know what a Burroughs computer is! )
2 - Each digit in the PIC occupies 1 half byte, or .5 full bytes
3 - The sign will occupy 1 half byte or .5 full bytes. (Even if it is unsigned, there is always a sign allocation)
Example: PIC S9(3) COMP-3.
PIC S9(3) COMP-3 will occupy 3 half bytes or 1.5 full bytes.
The sign occupies 1 half byte or .5 full bytes.
Therefore PIC S9(3) COMP-3 will occupy 2 full bytes.
Another example: PIC 9(4) COMP-3
A PIC 9(4) COMP-3 will occupy 4 half bytes, or 2 full bytes.
The sign (even when unsigned) will occupy 1 half byte, or .5 full bytes.
The sum of these two is 5 half bytes, or 2.5 full bytes.
Remember, the compiler always allocates in full bytes, therefore, the compiler will add 1 half byte, so PIC 9(4) will occupy 6 half bytes or 3 full bytes.
You use the formula n/2 + ? rounded up to a full byte.
n/2 = number of full bytes the digits from the PIC will use
? = the number of full bytes the sign will use
rounded to full byte = number of full bytes the compiler adds to allocate in full bytes only. This will be 0, or .5 only