带无符号字符的模运算符 [英] modulus operator with unsigned chars
问题描述
试图让一些代码工作而模数不想做我想要它做的事情......这意味着我错了.
我有 unsigned char
我试图将小时/分钟/秒分开,以便我可以在屏幕上以 Ascii 显示它们.
变量secs
是一个unsigned int
.其他一切都是无符号字符
.我想要 unsigned char
的结果,以免浪费内存.在嵌入式环境中工作.
有人愿意看看代码片段并告诉我我做错了什么吗?
hours = secs/3600.0;分钟 =(秒/60.0)-(小时*3600);秒=秒-(小时*3600)-(分钟*60);sec_ones =(unsigned char)((秒%10));sec_tens =(unsigned char)((seconds-sec_ones)%100);min_ones =(无符号字符)(分钟%10);min_tens =(unsigned char)((minutes-min_ones)%100);hrs_ones =(无符号字符)(小时%10);hrs_tens =(unsigned char)((hours-hrs_ones)%100);
minutes =(secs/60.0)-(hours*3600);
应该
minutes =(secs/60.0)-(hours*60);
除此之外,它适用于足够小的输入:http://ideone.com/VPKP1>
不过,有些事情我会改变.例如,进行双除法然后将结果分配回 unsigned char
是没有意义的,您还不如只进行整数除法.
Trying to get some code working and the modulus doesn't want to do what I want it to do... which means I have it wrong.
I have unsigned char
s that I'm trying to seperate out hours/minutes/seconds into so I can display them on the screen in Ascii.
The variable secs
is anunsigned int
. Everything else is anunsigned char
. I want the results in unsigned char
s so not to waste memory. Working in an embedded environment.
Anyone care to look at the code snippet and tell me what I've done wrong?
hours = secs/3600.0;
minutes =(secs/60.0)-(hours*3600);
seconds =secs-(hours*3600)-(minutes*60);
sec_ones =(unsigned char)((seconds%10));
sec_tens =(unsigned char)((seconds-sec_ones)%100);
min_ones =(unsigned char)(minutes%10);
min_tens =(unsigned char)((minutes-min_ones)%100);
hrs_ones =(unsigned char)(hours%10);
hrs_tens =(unsigned char)((hours-hrs_ones)%100);
minutes =(secs/60.0)-(hours*3600);
should be
minutes =(secs/60.0)-(hours*60);
Other than that, it works for small enough input: http://ideone.com/VPKP1
There are some things that I'd change, though. For example, there's no point doing a double division and then assigning the result back to unsigned char
, you might as well just do integer division.
这篇关于带无符号字符的模运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!