2

In my program two variables are declared as signed long (let say X and Y on 32 bit machine) and these divided one by other(X/Y).

The final value is assigned to a unsigned long variable(Let say Z). I am not sure whether this is right or wrong assignment. I am just debugging a code that was written by some one. I guess this may lead to overflow or undefined state.

What happens in below four scenarios,

Z =+X/+Y  
Z =+X/-Y  
Z =-X/+Y  
Z =-X/-Y

I know that %u for unsigned and %d for integer. My question is regarding what value would be stored in Z in the above four scenarios.

Any help would be greatly appreciated.

phuclv
  • 37,963
  • 15
  • 156
  • 475
rakeeee
  • 973
  • 4
  • 19
  • 44
  • 3
    why can't you try this yourself to have a first idea? – Jens Gustedt Feb 23 '12 at 12:23
  • 2
    This is not a question about division. It is a question about signed to unsigned conversion: http://stackoverflow.com/questions/50605/signed-to-unsigned-conversion-in-c-is-it-always-safe – David Heffernan Feb 23 '12 at 12:29
  • 3
    @JensGustedt Are you advocating programming by trial and error? I consider that to be poor advice. – David Heffernan Feb 23 '12 at 12:30
  • 1
    @David: yep, you can save a lot of time by not bothering with the trial. Get straight to the error ;-) I think it's reasonable to get a "first idea" by trying things. This first idea is one example of possible behavior, and as long as you don't assume that's the only possible behavior you should be good. – Steve Jessop Feb 23 '12 at 12:56
  • @DavidHeffernan, without trying out, people usually have a hard time even for telling us what they expect. What I would expect from such a question would be something like "I am interested in such and such" and "my compiler gives me that". "Because of this and that I was expecting something different, can you please tell me where I was wrong." – Jens Gustedt Feb 23 '12 at 13:21

3 Answers3

1

If your variables are signed, all is fine. Perhaps there is a (unwanted?) conversion afterwards if you gert a negative division result.

Working with expressions containing unsigned values is more painful, e.g.

(1U-2)/10

gives unexpected results.

glglgl
  • 89,107
  • 13
  • 149
  • 217
0

Z would store the value of the integer division, but as Z is unsigned, all values will be positive, and thus the sign bit will not be processed as such, but as part of the number, and also there will be no two's complement conversion. For example, if unsigned int is 32-bit wide:

X = 1, Y = 1 -> Z = 1
X = -1, Y = 1 -> Z = 4294967295 = 0xFFFFFFFF (this would be -1 -two's complement- if Z was signed)
m0skit0
  • 25,268
  • 11
  • 79
  • 127
0

You'll get garbage if result of division is negative.

For example:

unsigned z;
int a = 10;
int b = -2;
z = a/b;

then z == 4294967291.

neciu
  • 4,373
  • 2
  • 24
  • 33
  • I agree with you but how to control this situation without saving those unexpected results especially when one of them is negative(X and Y). – rakeeee Feb 23 '12 at 12:38
  • 1
    @raki What do you mean? What do you want to happen when you try to store a negative value in an unsigned type? – David Heffernan Feb 23 '12 at 12:41
  • @raki, I don't get exactly what you want. If a < 0 or b < 0 you'll have to handle this assigned is special way e.g. int x = (int) z – neciu Feb 23 '12 at 12:52
  • I just want to make sure that there would be no overflow or any other issues especially when result of the division is assigned to Z(unsigned). My problem is especially with any of them become negative(X or Y). This part of the code would run manytimes with different X and Y values which were taken by the PWM signal. – rakeeee Feb 23 '12 at 13:11
  • No, there will be no problem in casting negative result to unsigned but you have to be aware that your casted result can contain garbage. – neciu Feb 23 '12 at 14:24
  • 3
    This is just the result of converting -5 to an unsigned int. – S.S. Anne Apr 09 '20 at 20:11