CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

8 bit math question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
anestho



Joined: 27 Dec 2006
Posts: 28

View user's profile Send private message

8 bit math question
PostPosted: Fri Apr 27, 2007 8:48 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Apr 27, 2007 8:56 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Fri Apr 27, 2007 9:24 am     Reply with quote

Quote:
didactic

vocabulary++; Smile
anestho



Joined: 27 Dec 2006
Posts: 28

View user's profile Send private message

PostPosted: Fri Apr 27, 2007 10:00 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Apr 27, 2007 12:04 pm     Reply with quote

treitmey,

I guess I wrote something wrong in "Spanglish", I re-checked and fortunately is right.
At least this one! Thanks for the praise. Very Happy


Humberto
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Sun Apr 29, 2007 11:18 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Apr 30, 2007 3:21 pm     Reply with quote

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. Wink

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

View user's profile Send private message

PostPosted: Mon Apr 30, 2007 5:51 pm     Reply with quote

My mistake in posting. When I was dividing, I was using float. I know integers can't hold floats Embarassed

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

View user's profile Send private message

PostPosted: Mon Apr 30, 2007 9:03 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group