View previous topic :: View next topic |
Author |
Message |
anestho
Joined: 27 Dec 2006 Posts: 28
|
8 bit math question |
Posted: Fri Apr 27, 2007 8:48 am |
|
|
Hi,
If I have an 8 bit integer, it can hold 0-255. What happens when I add 1 to 255, will it wrap to 0 or will it cause an error?
int8 X
X = (200 * 200) >>7; // div by 128
What will X be?
The answer is 8 bit, but the calculation is 16 bit. What happens here. I need to know if I need to make X 16 bit, or is 8 bit enough. |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Fri Apr 27, 2007 8:56 am |
|
|
http://www.ccsinfo.com/faq.php?page=type_conversions
The best way to learn is trying yourself. To answer your question it is not necesary
to burn any PIC, I strongly suggest you to install MPLAB and run your test programs.
You will enable "to see" the variables behaviour step by step, it is very didactic,
hope you enjoy it.
Humberto |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Fri Apr 27, 2007 9:24 am |
|
|
vocabulary++; |
|
|
anestho
Joined: 27 Dec 2006 Posts: 28
|
|
Posted: Fri Apr 27, 2007 10:00 am |
|
|
I will test it out, but its bothering me now!!! and I can't test it.
I meant to write
unsigned int8 X;
X = (200 x 20) >> 7;
so: x = (4000) >> 7, and x = 31.25. This is within 0-255, but the math goes to 4000 in the intermediate calculation.
I think X will be 0, since the 4000, will push the answer above 8 bits. WHen the answer is bit shifted 7 bits, it will be 0.
Can someone confirm or refute this please. |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Fri Apr 27, 2007 12:04 pm |
|
|
treitmey,
I guess I wrote something wrong in "Spanglish", I re-checked and fortunately is right.
At least this one! Thanks for the praise.
Humberto |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Sun Apr 29, 2007 11:18 pm |
|
|
anestho wrote: | X = (200 x 20) >> 7; |
two 8 bit numbers
X = (200 x 20) >> 7;
X = (0xC8 x 0xC8) >>7;
X = 0x40 >>7;
(0xC8 x 0xC8) = 0x40
Int16(0xC8 x 0xC8) = 0x9C40 |
|
|
jds-pic
Joined: 17 Sep 2003 Posts: 205
|
|
Posted: Mon Apr 30, 2007 3:21 pm |
|
|
anestho wrote: |
unsigned int8 X;
X = (200 x 20) >> 7;
so: x = (4000) >> 7, and x = 31.25.
Can someone confirm or refute this please. |
i refute it.
x is an unsigned int8.
x will never equal 31.25, no matter how hard you try.
q.e.d.
ps:
you also need to explain to us how you got to 31.25 in the first place.
x = (4000>>7) = 31.
jds-pic |
|
|
anestho
Joined: 27 Dec 2006 Posts: 28
|
|
Posted: Mon Apr 30, 2007 5:51 pm |
|
|
My mistake in posting. When I was dividing, I was using float. I know integers can't hold floats
So basically if I do this it should do what I want:
int8 x;
x = int16(200 * 20) >> 7; |
|
|
jds-pic
Joined: 17 Sep 2003 Posts: 205
|
|
Posted: Mon Apr 30, 2007 9:03 pm |
|
|
anestho wrote: |
So basically if I do this it should do what I want:
int8 x;
x = int16(200 * 20) >> 7; |
IMHO, this is still poor syntax.
first, the compiler will evaluate/simplify the expression above to a constant (31), so it is not a good example at all.
second, casting is best done so that the left side ends up with the same width as the right, and there is no "implicit" casting operation left up to the compiler. in other words, do it, and do it right, showing to the compiler and to the person reading the code EXACTLY what you are trying to do. this makes your code more portable, more debuggable, and less cumbersome to review.
try it again. use x, y, and z to demonstrate.
assume,
int8 x;
int8 y;
int8 z;
now make this work, and work correctly, for all possible y and z.
x = (y * z) >> 7;
now then, consider the following
assume,
int8 x;
int16 y;
int16 z;
now make that work, and work correctly, for all possible y and z.
x = (y * z) >> 7;
jds-pic |
|
|
|