473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

comparing long integers

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
Nov 14 '05 #1
11 6625
In article <e0**************************@posting.google.com >, Elijah Bailey wrote:
I tried splitting the array into longs and comparing, but then
I face the high endian/low endian problem on some machines.


If you're worried about that, you would need to write "conditional code"
that depends on endianness (for example, using #ifdef or templates if you
like) and make endianness a compile-time configurable option (depending on
your tools, you could automate detection/configuration of this)

Anyway, that's what I do whenever I write code that is endian-dependent
(usually applies to code that reads binary data from disk)

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Nov 14 '05 #2
Elijah Bailey wrote:

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)


If the arrays were in most-to-least significant
order you could use the memcmp() function. This is
likely to be as least as fast as anything you could
write in plain C, if not faster (although, of course,
the Standard makes no such guarantee). So if you are
able to redesign things so the arrays appear 'tother
way around, it might be worth while to do so.

Failing that, you could roll your own:

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])
return (p[n] < q[n]) ? -1 : +1;
}
return 0;
}

--
Er*********@sun.com
Nov 14 '05 #3
On 22 Jan 2004 13:39:52 -0800, ge******@hotmail.com (Elijah Bailey)
wrote:
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


<<Remove the del for email>>
Nov 14 '05 #4
Eric Sosman <Er*********@sun.com> wrote in message news:<40***************@sun.com>...
Elijah Bailey wrote:

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)


If the arrays were in most-to-least significant
order you could use the memcmp() function. This is
likely to be as least as fast as anything you could
write in plain C, if not faster (although, of course,
the Standard makes no such guarantee). So if you are
able to redesign things so the arrays appear 'tother
way around, it might be worth while to do so.

Failing that, you could roll your own:

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])
return (p[n] < q[n]) ? -1 : +1;
}
return 0;
}


Neither memcmp nor Eric's rev_memcmp will compare chars as integers,
rather they compare raw bytes, so negative (plain) char values would
typically compare 'higher' than positive values.

--
Peter
Nov 14 '05 #5

If I understand the problem statement,
the original poster wants to compare the sums(totals)
of arrayA vs. arrayB. I see no way but to sum both
arrays and compare the values.

There is no language construct that I know of to do it
for him.

Silverlock

Peter Nilsson wrote:
Eric Sosman <Er*********@sun.com> wrote in message news:<40***************@sun.com>...
Elijah Bailey wrote:
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)


If the arrays were in most-to-least significant
order you could use the memcmp() function. This is
likely to be as least as fast as anything you could
write in plain C, if not faster (although, of course,
the Standard makes no such guarantee). So if you are
able to redesign things so the arrays appear 'tother
way around, it might be worth while to do so.

Failing that, you could roll your own:

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])
return (p[n] < q[n]) ? -1 : +1;
}
return 0;
}

Neither memcmp nor Eric's rev_memcmp will compare chars as integers,
rather they compare raw bytes, so negative (plain) char values would
typically compare 'higher' than positive values.


--
"It is impossible to make anything foolproof because fools are so
ingenious" - A. Bloch

Nov 14 '05 #6
ge******@hotmail.com (Elijah Bailey) wrote in message news:<e0**************************@posting.google. com>...
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?


Please don't cross-post questions like this to groups
that discuss different languages - it's likely to end
up with a confusing thread. If you need the question
answered for both languages, this is one of the
occasions where posting the same question to each
group in separate messages would be appropriate.
Nov 14 '05 #7
If you time this code compared for n = 8 or 16 and
instead use 2 or 4 longs, and then use the longs for
comparison instead of char *, you would see that
the speed of the long comparison is at least twice
as fast. Is there a way to speed this loop further
if k = 8?

Eric Sosman <Er*********@sun.com> wrote in message news:<40***************@sun.com>...
Elijah Bailey wrote:

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)


If the arrays were in most-to-least significant
order you could use the memcmp() function. This is
likely to be as least as fast as anything you could
write in plain C, if not faster (although, of course,
the Standard makes no such guarantee). So if you are
able to redesign things so the arrays appear 'tother
way around, it might be worth while to do so.

Failing that, you could roll your own:

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])
return (p[n] < q[n]) ? -1 : +1;
}
return 0;
}

Nov 14 '05 #8
Nick Landsberg wrote:

If I understand the problem statement,
the original poster wants to compare the sums(totals)
of arrayA vs. arrayB. I see no way but to sum both
arrays and compare the values.

There is no language construct that I know of to do it
for him.


Kindly do not toppost. Doing so has lost all sensible quotation.

As I understood it he wanted to compare strings holding numerical
quantities, i.e. the output of (typically) sprintf("%d", n); But
it was far from clear.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #9
Elijah Bailey wrote:

If you time this code compared for n = 8 or 16 and
instead use 2 or 4 longs, and then use the longs for
comparison instead of char *, you would see that
the speed of the long comparison is at least twice
as fast. Is there a way to speed this loop further
if k = 8?


Kindly do not toppost. Doing so has lost all context, and makes
your query useless.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #10
In <bu**********@216.39.135.13> Barry Schwarz <sc******@deloz.net> writes:
On 22 Jan 2004 13:39:52 -0800, ge******@hotmail.com (Elijah Bailey)
wrote:
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.


Well, strncmp is useless for this purpose. If both arrays are identical
up to (and including) the first null byte, it would declare them equal...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11
In <40***************@yahoo.com> CBFalconer <cb********@yahoo.com> writes:
Nick Landsberg wrote:

If I understand the problem statement,
the original poster wants to compare the sums(totals)
of arrayA vs. arrayB. I see no way but to sum both
arrays and compare the values.

There is no language construct that I know of to do it
for him.


Kindly do not toppost. Doing so has lost all sensible quotation.

As I understood it he wanted to compare strings holding numerical
quantities, i.e. the output of (typically) sprintf("%d", n); But
it was far from clear.


As usual, you forgot to engage your brain. It was crystal clear:

My array has data such that the 1st byte is the least significant
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
byte (and the kth byte is the most significant byte)
^^^^

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: beliavsky | last post by:
By mistake I coded something like print ("1" > 1) and got the result "True". Comparing an integer and a string seems meaningless to me, and I would prefer to have an exception thrown. Can...
13
by: Jeff Melvaine | last post by:
I note that I can write expressions like "1 << 100" and the result is stored as a long integer, which means it is stored as an integer of arbitrary length. I may need to use a large number of...
12
by: Elijah Bailey | last post by:
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...
13
by: Nicholas | last post by:
How can I compare char* with integers and characters contained in the str, where integers can be one digit or more? void Access(char *str) { char *pt = str; while (pt != '0') { if...
11
by: John | last post by:
Hi, I encountered a strange problem while debugging C code for a Windows-based application in LabWindows CVI V5.5, which led me to write the test code below. I tried this code with a different...
89
by: purifier | last post by:
The problem is to write a program in 'C' to find the greatest of 2 given numbers... Easy? huh here's the catch do not use 'if' or any conditional statements if u want it to be a little more...
1
by: Donald Grove | last post by:
If I have two arrays, what is a good paradigm for comparing what is in them, to determine what elements they share, or don't share? Specifically, each array could potentially contain the integers...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
10
by: arnuld | last post by:
It is quite an ugly hack but it is all I am able to come up with for now :-( and it does the requires work. I want to improve the program, I know you people have much better ideas ;-) /* C++...
1
by: RN1 | last post by:
Sometimes I find that though I am comparing 2 integers, the result turns out to be unexpected. For e.g. an ASP page encapsulates recordset paging. <% Dim iPage,iPageCounter ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.