比较长整数 [英] comparing long integers

查看:110
本文介绍了比较长整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个大小为k的字符数组。

我想知道哪一个更大(就像实例一样

我比较两个整数/多头/等等。 )。

最快的方法是什么? k <= 10通常是我的申请。

我试过bcmp / a循环进行比较,但看起来它们很慢

与比较多头...想法?


我尝试将数组拆分成long并进行比较,但是然后

我在某些机器上面临高端/低端问题。

我的数组有数据,第一个字节是最不重要的
字节(第k个字节是最重要的字节)


谢谢,

--Elijah

I have two char arrays of size k.
I want to know which one is bigger (exactly like for instance
I compare two ints/longs/etc.).

What is the fastest way to do this? k <= 10 usually for my application.
I tried bcmp / a loop for comparison, but it seems they are very slow
compared to comparing longs...Any ideas?

I tried splitting the array into longs and comparing, but then
I face the high endian/low endian problem on some machines.
My array has data such that the 1st byte is the least significant
byte (and the kth byte is the most significant byte)

Thanks,
--Elijah

推荐答案

文章< e0 *********** ***************@posting.google.com>,Elijah Bailey写道:
In article <e0**************************@posting.google.com >, Elijah Bailey wrote:
我尝试将数组拆分成long并进行比较,但是后来我在某些机器上遇到了高端/低端问题。
I tried splitting the array into longs and comparing, but then
I face the high endian/low endian problem on some machines.




如果你担心这个问题,你需要写 ;条件代码

取决于字节顺序(例如,我们如果你喜欢#ifdef或模板,并且使endianness成为编译时可配置的选项(取决于你的工具,你可以自动检测/配置这个)


无论如何,这就是我每次编写依赖于字节序的代码时所做的事情

(通常适用于从磁盘读取二进制数据的代码)


干杯,

-

Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/


以利亚Bailey写道:
Elijah Bailey wrote:

我有两个大小为k的字符数组。
我想知道哪一个更大(完全像是比较
我比较两个整数/渴望等等。

最快的方法是什么? k <= 10通常适用于我的应用。
我尝试使用bcmp / a循环进行比较,但看起来它们非常慢
与比较longs相比...任何想法?
我的数组有数据,第一个字节是最不重要的
byte(和第k个字节是最重要的字节)

I have two char arrays of size k.
I want to know which one is bigger (exactly like for instance
I compare two ints/longs/etc.).

What is the fastest way to do this? k <= 10 usually for my application.
I tried bcmp / a loop for comparison, but it seems they are very slow
compared to comparing longs...Any ideas?

I tried splitting the array into longs and comparing, but then
I face the high endian/low endian problem on some machines.
My array has data such that the 1st byte is the least significant
byte (and the kth byte is the most significant byte)




如果数组是最重要的,那么
命令你可以使用memcmp()函数。这是

可能至少和你能做到的一样快......

用简单的C写,如果不是更快(当然,

标准没有这样的保证)。所以,如果你能够重新设计东西,那么阵列似乎b / b
,那么这样做可能是值得的。

如果做错了,你可以自己动手:


int rev_memcmp(const void * pp,const void * qq,size_t n)

{

const unsigned char * p = pp,* q = qq;

while(n - > 0){

if( p [n]!= q [n])

返回(p [n]< q [n])? -1:+ 1;

}

返回0;

}


-
Er*********@sun.com


2004年1月22日13:39:52 -0800, ge ****** @ hotmail .com (Elijah Bailey)

写道:
On 22 Jan 2004 13:39:52 -0800, ge******@hotmail.com (Elijah Bailey)
wrote:
我有两个大小为k的char数组。
我想知道哪个一个更大(完全像例如
我比较两个整数/长/等)。


你不喜欢标准的memcmp和strncmp功能。

最快的方法是什么? k <= 10通常适用于我的应用。
我尝试使用bcmp / a循环进行比较,但看起来它们非常慢
与比较longs相比...任何想法?
我的数组有数据,第一个字节是最不重要的
字节(和第k个字节是最重要的字节)

谢谢,
- 以利亚
I have two char arrays of size k.
I want to know which one is bigger (exactly like for instance
I compare two ints/longs/etc.).
What don''t you like about the standard memcmp and strncmp functions.

What is the fastest way to do this? k <= 10 usually for my application.
I tried bcmp / a loop for comparison, but it seems they are very slow
compared to comparing longs...Any ideas?

I tried splitting the array into longs and comparing, but then
I face the high endian/low endian problem on some machines.
My array has data such that the 1st byte is the least significant
byte (and the kth byte is the most significant byte)

Thanks,
--Elijah




<< ;删除电子邮件的del>>



<<Remove the del for email>>


这篇关于比较长整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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