将短整数的前 3 位和后 3 位拼接起来?
我的数组中有很多变量,如下所示:short num = 7123;
。该值的长度始终为 4 位数字。如何将其变成 a = 7; b = 123;
?
我能想到的就是转换为 c 字符串并将其剥离,但似乎效率不高。
I have lots of variables in an array like this: short num = 7123;
. The value is ALWAYS 4 digits long. How to go about turning this into a = 7; b = 123;
?
All I can think of is converting to c-string and stripping it off, but doesn't seem efficient.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
C 标准库包含
div()
,它可以在一个操作中完成此操作:C 标准库预计有一个
div()
的优化实现,它将执行以下操作:除法和余数在一条机器指令中(在具有此类指令的 CPU 上)。The C standard library contains
div()
which can do this in one operation:The C standard library can be expected to have an optimised implementation of
div()
that will do the division and remainder in one machine instruction (on CPUs that have such an instruction).很简单:
It's as simple as: