We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi All, i'm having a unique problem while taking the modulus of a number. please could any one help me out.
i)8432648 Mod 1048576 = 44040 i get 44040
ii)8498184 Mod 1048576 = 109576 i get 44040
iii)8563720 Mod 1048576 = 175112 iget 109576
iv)8629256 Mod 1048576 = 240648 i get 175112
v)8694792 Mod 1048576 = 306184 i get 240648
vi)8760328 Mod 1048576 = 371720 i get 306184
vii)8825864 Mod 1048576 = 437256 i get 437256
viii)8891400 Mod 1048576 = 502792 i get 437256
this just goes on. if u see the i) & vii) case i get the desired value but in the rest i get the value of privious number.
i am using aa=(aa%1048576); where aa is long int. and has values i denoted.
please could any one help me out with this.
Thanks Ajay
I can't tell you why you are getting those results, but here's a workaround:
if (aa >= 0) { aa = aa & 0xFFFFFL; } else { aa = -aa & 0xFFFFFL; aa = -aa; }
Hi, Thanks for your reply, I used another method and still i got the same problem that is when i realised that my problem was someplace else. I checked all the values that were being returned in the array below but i did not actualy check the value of aa i blindly assumed it was correct. aa is actualy giving worng output i do not know why.
it is a long int will it overflow?? aa=(keyCode[7]+(keyCode[6]*10)+(keyCode[5]*100)+(keyCode[4]*1000) +(keyCode[3]*10000)+(keyCode[2]*100000)+ (keyCode[1]*1000000)+(keyCode[0]*10000000)); 8694791 what the value should be 8629255 this is what is stored in aa. i checked and double checked keycode it is giving the right sequence but is faling in combining the output.
Suffix all those constants with an 'L'; for example 1000 becomes 1000L.
A literal constant in C has the type "int" unless the compiler is told otherwise. "Int" in C51 happens to be 16 bits. This can affect the width of your integer operations.
Literals that are too big to fit into an int will be treated as a long (in C51, 32 bits) "10000" and "100000" do not have the same type.
The "L" suffix forces the compiler to treat the literal as a "long" even if the actual value would fit into 16 bits.
You can also use a "U" to note that the literal is unsigned: 65535U, 4294967295UL.
Hi Drew & Dan, So let me see if i am interpreting this right all i have to do Take this code aa=(keyCode[7]+(keyCode[6]*10)+(keyCode[5]*100)+(keyCode[4]*1000) +(keyCode[3]*10000)+(keyCode[2]*100000)+ (keyCode[1]*1000000)+(keyCode[0]*10000000)); and replace it with this aa=(keyCode[7]+(keyCode[6]*10L)+(keyCode[5]*100L)+(keyCode[4]*1000) +(keyCode[3]*10000L)+(keyCode[2]*100000L)+ (keyCode[1]*1000000L)+(keyCode[0]*10000000L));
and it will convert all to long and addtion will be correct.
Hi, Thank you very much both of you guys. it worked. Thanks a lot, Ajay