[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vacets-tech] Computer Arithmetic in C/C++



Vu+-o+-ng,

o+-? -da^y no'i ve^` Integer only.
if you use floating point is different story.

-du'ng ra ca^u ho?i more general ho+-n la` chi? assign 66,000.

co' nghi+AH4-a mi`nh muo^'n la`m addition, subtraction, mulitplication etc.

-die^`u na`y hay xa^?y ra trong old micro-processor hay small microcontroller
khi ma` kha? na(ng
arithmetic ra^'t gio+-'i ha.n vi du. nhu+- Intel 8051 family, Z80  etc

vi' du. o+-? -da^y no'i ve^` ti'nh co^.ng (to extend 16 bits integer to 32 bits
by using two 16 bits)
trong Assembly language mi`nh chi? ca^`n  du`ng an instruction nhu+- ADDC (add
with carry) khi co^.ng
the most significant 16 bits la` -du?.

nhu+- You hay anh toa`n -da+AH4- no'i la` du`ng two 16 bits (create user-defined
type)
va` overload OPERATOR +-

  typedef struct _UNIT32{
        UINT16 msb;
        UINT16 lsb;
   } UINT32;

UINT32  num1, num2, num3;

num1 = 0x0000FFFF  (num1.lsb = 0xFFFF, num1.msb = 0x0001)
num2 = 0x00000001   (num2.lsb = 0x0000, num2.msb = 0x0000)

nhu+- va^.y

num3 = num1 +- num2

should be  num3 = 0x00010000

an overload +- operation co' the^? :

  num3.lsb = num1.lsb +- num2.lsb
  num3.msb = num1.msb +- num2.msb

how would you keep track the OVERFLOW  of  (num1.lsb +- num2.lsb)

looking for an +-  function in C .
I'm too lazy (perhaps do not have time nor patience) to figure it out :))

Anyway it says that certain things can be done very easily in ASSEMBLY language

but seem to be cumbersome in a high-level language.

Danh.






Anh Vuong wrote:

> [vacets-tech] From Anh Vuong <anh_vuong@yahoo.com>
>
>    Hello Danh,
>
> --- Paul Pham <phamp@vinet.com> wrote:
> > [vacets-tech] From Paul Pham <phamp@vinet.com>
> > hello ba` con.
> >
> > in C, gia? su+-? mi`nh co' :
> >
> >             unsigned long  number;
> >
> > the amount of storage used to hold UNSIGNED LONG is
> > machine-dependent.
> >
> > gia? su+-?  UNSIGNED LONG o+-? -da^y -du+-o+-.c
> > allocated
> > maximun 16 bits.
> >
> > with 16 bits UNSIGNED nhu+- va^.y   0 <=  number <=
> > 65535
> >
> > gia? su+-? mi`nh muo^'n assigned 66000 thi` la`m sao
> > ??
>
>    Du`ng another variable type, ie. unsigned double
> (if there is one); Or you can write a function to do
> the conversion back and forth. For example you can use
> two unsigned long variables, each has the 16-bit
> limit, then use the shift and mask operations to
> assign or extract the values. In C you can define
> your own class to handle this.
>
>    Cheers,
>    Ly
> ---
>
> >
> > trong assembly language -die^`u na`y tu+-o+-ng
> > -do^'i de^+AH4-. Nhu+-ng trong
> > C/C+-+-
> > thi` kho^ng -do+-n gia?n.
> >
> > Danh.
> >
>
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com
>
> ----------------------------
>
> To subscribe/unsubscribe to vacets-tech, send email to majordomo@vacets.org
> with one of the following text lines:
>
> subscribe vacets-tech
> or
> unsubscribe vacets-tech