Signed division in C

Programming, for all ages and all languages.
Post Reply
kestaz
Posts: 11
Joined: Thu Jan 10, 2008 6:09 pm

Signed division in C

Post by kestaz »

How to write such code in C:

Temporary = AX / Source; //signed division
AL = Temporary;
AH = AX % Source; //signed modulus

I get ax, al, ah values which is by default unsigned, to perform signed divide i just need to convert them to signed and that's all ?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Signed division in C

Post by Combuster »

since when would dividing signed numbers be done with an unsigned division? :roll:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
kestaz
Posts: 11
Joined: Thu Jan 10, 2008 6:09 pm

Re: Signed division in C

Post by kestaz »

I don't understand your question?. I'm need to implement div and idiv instructions, but i don't see difference.

I found page: http://www.ragestorm.net/blogs/?p=62 it's talking about. Says to signed divide i need to do:

res = x/y; if (sign_of_x != sign_of_y) res = -res; (need do this in python)

Does this same in C ?
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

Re: Signed division in C

Post by CodeCat »

You need to write division in C? It might help if you told us a bit more about the requirements/restrictions. Otherwise I'd just say, why bother? Just use C's built in division.
Post Reply