修正了BCD码。 [英] Fixed BCD code.

查看:81
本文介绍了修正了BCD码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我做了第一次尝试的哈希,并且努力提供

PORTABLE代码来解决问题,我提交此解决方案转换为

以及存储在long中的BCD值。对于

整数和(理论上)字符以及实际上任何整数类型都应该如此。如果它没有,

建议改进它的方法,即使是在显着的速度/代码大小惩罚。


#include< math.h> ;


长bcd(长十进制)

{

int i;

long result = 0;


for(i = 0;十进制; ++ i){

结果+ =(十进制%10)*(int)pow(16 ,i);

十进制/ = 10;

}


返回(结果);

}


长dec(长bcd)

{

长结果= 0;

int i;


for(i = 0; bcd; ++ i){

result + =(bcd%16)*(int)pow( 10,i);

bcd / = 16;

}


返回(结果);

}


/ *在一台机器上测试代码,但理论上C是可移植的。 * /

Cause I made such a hash of my first attempt, and in an effort to provide
PORTABLE code to solve the problem, I submit this solution to convert to
and from BCD values stored in longs. This should work just as well for
ints and (in theory) chars and, indeed, any integral type. If it doesn''t,
suggest ways to improve it, even at a significant speed/code size penalty.

#include <math.h>

long bcd(long decimal)
{
int i;
long result = 0;

for(i = 0; decimal; ++i) {
result += (decimal % 10) * (int) pow(16,i);
decimal /= 10;
}

return(result);
}

long dec(long bcd)
{
long result = 0;
int i;

for(i = 0; bcd; ++i) {
result += (bcd % 16) * (int) pow(10,i);
bcd /= 16;
}

return(result);
}

/* Code tested on all of one machine, but C is portable in theory. */

推荐答案

在文章< Xn ****************** ****************@63.223.5.101>,

August Derleth< li ************** ***@onewest.net>写道:
In article <Xn**********************************@63.223.5.101 >,
August Derleth <li*****************@onewest.net> wrote:
因为我做了我的第一次尝试的哈希,并努力提供
PORTABLE代码来解决问题,我提交此解决方案转换为
和来自存储在long中的BCD值。这对于
整数和(理论上)字符以及实际上任何整数类型都应该起作用。如果它没有,
建议改进它的方法,即使在显着的速度/代码大小惩罚。

#include< math.h>
长bcd(长十进制)
{
int i;
long result = 0;

for(i = 0; decimal; ++ i){<结果+ =(十进制%10)*(int)pow(16,i);


pow(16,i)返回一个double,然后将其转换为int。

无法保证其精度和长度一样多。相反:

结果+ =(十进制%10)<< (4 * i);

(还提供不转换来回的性能优势
浮点数和整数之间的
。)


出于类似的原因:


长dec(长bcd)

{

长结果= 0,i = 1 ;


而(bcd){

结果+ = bcd%16 * i;

i * = 10;

bcd / = 16;

};

}

/ *代码在所有一台机器上测试,但C是理论上是便携的。 * /
Cause I made such a hash of my first attempt, and in an effort to provide
PORTABLE code to solve the problem, I submit this solution to convert to
and from BCD values stored in longs. This should work just as well for
ints and (in theory) chars and, indeed, any integral type. If it doesn''t,
suggest ways to improve it, even at a significant speed/code size penalty.

#include <math.h>

long bcd(long decimal)
{
int i;
long result = 0;

for(i = 0; decimal; ++i) {
result += (decimal % 10) * (int) pow(16,i);
pow(16, i) returns a double, which you then cast to an int. There is
no guarantee that either has as much precision as a long. Instead:
result += (decimal %10) << (4 * i);
(Also offers the performance benefit of not converting back and forth
between floating point and integer.)

For similar reasons:

long dec (long bcd)
{
long result = 0, i = 1;

while (bcd) {
result += bcd % 16 * i;
i *= 10;
bcd /= 16;
};
}
/* Code tested on all of one machine, but C is portable in theory. */




只有当你没有对标准的实现定义

部分做出假设时。


- Brett



Only when you don''t make assumptions about implementation-defined
portions of the standard.

-- Brett


rb*@panix.com ( Brett Frankenberger)写了

新闻:bj ********** @ reader2.panix.com于2003年9月5日星期五09:04:00p:
rb*@panix.com (Brett Frankenberger) wrote in
news:bj**********@reader2.panix.com on Fri 05 Sep 2003 09:04:00p:
文章< Xn ********************************** @ 63.223.5.101> ,
August Derleth< li ***************** @ onewest.net>写道:
In article <Xn**********************************@63.223.5.101 >,
August Derleth <li*****************@onewest.net> wrote:
for(i = 0; decimal; ++ i){result + =(decimal%10)*(int)
pow(16,i);
for(i = 0; decimal; ++i) { result += (decimal % 10) * (int)
pow(16,i);



pow(16,i)返回一个double,然后将其转换为int。没有保证要么具有长的精度。相反:
结果+ =(十进制%10)<< (4 * i);



pow(16, i) returns a double, which you then cast to an int. There is
no guarantee that either has as much precision as a long. Instead:
result += (decimal %10) << (4 * i);




啊,但这些转换是否可以移植到那些使用,例如,倍数为
的三倍的机器上字的大小?我的倍数会增加吗?


我无法想象BCD如何在八进制系统中运行(你只需要b / b
就有三位编码每个数字*),或假设三元

系统(/好/ C代码应该是可移植的)。


是问题本身仅限于某些类型的系统?


*三位只能给你八个数字。 9位是8 + 1,为您提供编码一个'传统''BCD数字的可能性,可以选择

符号位。你可以从那里做数学。



Ah, but will the shifts be portable to machines that use, say, multiples
of three for their word-size? Will my multiplies?

I can''t think of how BCD would work in an octal system (where you''d only
have three bits to encode each digit*), or on a hypothetical ternary
system (which /good/ C code is supposed to be portable to).

Is the problem itself limited to certain kinds of systems?

*Three bits only gives you eight numbers. Nine bits is 8+1, giving you the
possibility of encoding one `traditional'' BCD digit with the option of a
sign bit. You can do the math from there.

/ *在所有一台机器上测试代码,但理论上C是可移植的。 * /
/* Code tested on all of one machine, but C is portable in theory. */



只有当你没有对标准的实现定义的部分做出假设时。



Only when you don''t make assumptions about implementation-defined
portions of the standard.




Touche''。



Touche''.


这篇关于修正了BCD码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆