473,890 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6733
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 "conditiona l 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(cons t 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******@hotmai l.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*********@su n.com> wrote in message news:<40******* ********@sun.co m>...
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(cons t 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*********@su n.com> wrote in message news:<40******* ********@sun.co m>...
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(cons t 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******@hotmai l.com (Elijah Bailey) wrote in message news:<e0******* *************** ****@posting.go ogle.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*********@su n.com> wrote in message news:<40******* ********@sun.co m>...
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(cons t 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********@yah oo.com) (cb********@wor ldnet.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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #10

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

Similar topics

5
7610
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 someone explain why Python allows comparisons between integers and strings, and how it handles those cases? Why is "1" > 1? Pychecker does not warn about the line of code above -- I wish it did.
13
2550
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 these, and am interested to know whether the storage efficiency of long integers is in danger of breaking my code if I use too many. Would I do better to write a class that defines bitwise operations on arrays of integers, each integer being assumed...
12
885
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 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.
13
6155
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 (isalpha(*pt)) printf("A character is found\n");
11
2405
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 compiler and got the same erroneous result on two different PCs (with OS Win98 & Win98SE), so it appears to be a problem with ANSI C. I thought that negative double variables could be compared as easily and *reliably* as integers, but apparently...
89
3502
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 tougher you can use the if but this time no relational operators or any of the predefined functions.... Can someone please help me solve the problem....
1
2249
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 1 to 9, but probably not all 9 integers. MyArray1 = 1,2,3,5,6,7,8 MyArray2 = 1,4,6,7 I would need the answers to be arrays too:
2
3395
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 checkInteger(&$value, $checks) { $err = ''; if (!is_numeric($value) || (floatval($value) != intval($value))) { $err .= 'Input must be an integer. ';
10
5039
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++ Primer - 4/e * * exercise 9.20 * STATEMENT: * Write a program to to compare whether a vector<intcontains the
1
4394
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 iPage=Request.QueryString("Page") If(iPage="") Then
0
9815
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11215
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10801
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10449
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9616
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8007
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5834
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6034
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.