473,761 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determine the maximum of two integers.

The following function determines the maximum of two integers. It
works on my machine.

If (a[0] - a[1]) is negative, what's the first bit of: (unsigned)(a[0]
- a[1])? Is it 0 or 1?
#include <limits.h>

int max(int n1, int n2)
{
int a[2];
a[0] = n1, a[1] = n2;
return a[((unsigned)(a[0] - a[1]) >sizeof n1 * CHAR_BIT - 1)];
}
Thank you for your time
Nov 18 '07 #1
29 3892
lovecreatesbea. ..@gmail.com wrote:
The following function determines the maximum of two integers. It
works on my machine.

If (a[0] - a[1]) is negative, what's the first bit of: (unsigned)(a[0]
- a[1])? Is it 0 or 1?
#include <limits.h>

int max(int n1, int n2)
{
int a[2];
a[0] = n1, a[1] = n2;
return a[((unsigned)(a[0] - a[1]) >sizeof n1 * CHAR_BIT - 1)];
}
Thank you for your time
This will work for twos complement and sign-magnitude machines. I doubt
you'll ever encounter any other sort.

It will fail on ones complement machines where the subtraction gave the
value -0, and it will fail on any other sort of representation, for
example excess-N.

However its a very complicated way to compare two ints. If I found it
during code review, I'd force my developer to remove it.

By the way, this isn't a C question. Ask in comp.programmin g next time.
Nov 18 '07 #2
On Sun, 18 Nov 2007 12:33:32 +0000, Mark McIntyre wrote:
lovecreatesbea. ..@gmail.com wrote:
>The following function determines the maximum of two integers. It works
on my machine.

If (a[0] - a[1]) is negative, what's the first bit of: (unsigned)(a[0]
- a[1])? Is it 0 or 1?
#include <limits.h>

int max(int n1, int n2)
{
int a[2];
a[0] = n1, a[1] = n2;
return a[((unsigned)(a[0] - a[1]) >sizeof n1 * CHAR_BIT - 1)];
}
Thank you for your time

This will work for twos complement and sign-magnitude machines. I doubt
you'll ever encounter any other sort.

It will fail on ones complement machines where the subtraction gave the
value -0,
-0 is positive zero, and when the subtraction generates that, there are
no extra problems.

The subtraction is only allowed to give negative zero when both n1 and n2
are zero, and one or both of them is negative. In this case, the cast to
unsigned gives plain zero, meaning a[0] would be returned, which is a
valid option when n1 == n2. Negative zero is not smaller or larger than
positive zero for C integers, even though other systems may differ.
and it will fail on any other sort of representation, for
example excess-N.
Such a representation is not allowed.

The code may also fail if unsigned int has padding bits, by the way, for
at least two reasons. Firstly, there is no guarantee that
UINT_MAX INT_MAX. Secondly, the behaviour of the right-shift is
undefined.
Nov 18 '07 #3
�Harald van Dijk wrote:
-0 is positive zero, and when the subtraction generates that, there are
no extra problems.
You sure? The bitpattern of -0 is all-ones I think.
>and it will fail on any other sort of representation, for
example excess-N.

Such a representation is not allowed.
By a conforming C compiler.
The code may also fail if unsigned int has padding bits, by the way, for
at least two reasons. Firstly, there is no guarantee that
UINT_MAX INT_MAX. Secondly, the behaviour of the right-shift is
undefined.
I belive UINT_MAX must be at least equal to INT_MAX however?
Nov 18 '07 #4
On Nov 18, 3:07 pm, Mark McIntyre <markmcint...@s pamcop.netwrote :
I belive UINT_MAX must be at least equal to INT_MAX however?
UINT_MAX >= INT_MAX >= 32767

x >= y does not guarantee x y.
Nov 18 '07 #5
On Sun, 18 Nov 2007 13:07:55 +0000, Mark McIntyre wrote:
�Harald van Dijk wrote:
>-0 is positive zero, and when the subtraction generates that, there are
no extra problems.

You sure? The bitpattern of -0 is all-ones I think.
The bit pattern of -0 is all zeroes, since -0 is plain old zero. The bit
pattern of ~0 is all ones, and that is allowed to be negative zero. You
can't create negative zero except by bit manipulation; it's simply not
allowed, even if it would make sense for the processor.
>>and it will fail on any other sort of representation, for example
excess-N.

Such a representation is not allowed.

By a conforming C compiler.
Yes.
>The code may also fail if unsigned int has padding bits, by the way,
for at least two reasons. Firstly, there is no guarantee that UINT_MAX
INT_MAX. Secondly, the behaviour of the right-shift is undefined.

I belive UINT_MAX must be at least equal to INT_MAX however?
Correct.
Nov 18 '07 #6
vi******@gmail. com wrote:
On Nov 18, 3:07 pm, Mark McIntyre <markmcint...@s pamcop.netwrote :
>I belive UINT_MAX must be at least equal to INT_MAX however?

UINT_MAX >= INT_MAX >= 32767

x >= y does not guarantee x y.
I'm aware of that. Hence the words "at least equal".

Nov 18 '07 #7
�Harald van Dijk wrote:
On Sun, 18 Nov 2007 13:07:55 +0000, Mark McIntyre wrote:
>�Harald van Dijk wrote:
>>-0 is positive zero, and when the subtraction generates that, there are
no extra problems.
You sure? The bitpattern of -0 is all-ones I think.

The bit pattern of -0 is all zeroes, since -0 is plain old zero. The bit
pattern of ~0 is all ones, and that is allowed to be negative zero. You
can't create negative zero except by bit manipulation; it's simply not
allowed, even if it would make sense for the processor.
Euh, I think we're at cross purposes.
-0 is negative zero
+0 is positive zero, plain old zero.

Not a C question however.
FUs set to

http://en.wikipedia.org/wiki/Ones_co....27_complement

:-)
Nov 18 '07 #8
On Sun, 18 Nov 2007 14:42:35 +0000, Mark McIntyre wrote:
�Harald van Dijk wrote:
>On Sun, 18 Nov 2007 13:07:55 +0000, Mark McIntyre wrote:
>>�Harald van Dijk wrote:
-0 is positive zero, and when the subtraction generates that, there
are no extra problems.
You sure? The bitpattern of -0 is all-ones I think.

The bit pattern of -0 is all zeroes, since -0 is plain old zero. The
bit pattern of ~0 is all ones, and that is allowed to be negative zero.
You can't create negative zero except by bit manipulation; it's simply
not allowed, even if it would make sense for the processor.

Euh, I think we're at cross purposes. -0 is negative zero
+0 is positive zero, plain old zero.
In C (which is the topic of this newsgroup), -0 is an expression which
evaluates to positive zero. Always, regardless of the representation of
integers. It was obvious that you meant negative zero, and I did address
the message as if you had said negative zero later on, but -0 is not a
good notation for it here.
Nov 18 '07 #9
Mark McIntyre wrote:
�Harald van Dijk wrote:
>-0 is positive zero, and when the subtraction generates that, there
are no extra problems.

You sure? The bitpattern of -0 is all-ones I think.
You think?

Gosh McIntyre!

-0 is minus zero is zero!

And that is a bit pattern of all ZEROS!

~0 is a bit pattern of all ones!

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Nov 18 '07 #10

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

Similar topics

9
2624
by: Till Crueger | last post by:
Hi, I have to implement some simple sorting algorithm. I am NOT asking for you to do my homework, but my question is rather on how to store the integers. I recall reading once in here that there is a maximum value the operator has to accept. Since the programm should accept almost any number of integers, I am not sure if this limit will be enough. Also I would like to know the same about the STL-Vector class, since I haven't decided...
3
36898
by: yawnmoth | last post by:
I'm trying to figure out how big integers in PHP can be and am having some difficulty. My first idea was to try something like this: <? for ($bits=0; (1<<($bits+1)) > (1<<$bits); $bits++); echo $bits; ?>
12
1467
by: Steve R. Hastings | last post by:
I was looking at a Python function to find the maximum from a list. The original was more complicated; I simplified it. The built-in max() function can replace the simplified example, but not the original. def maximum(lst): maxval = lst for i in xrange(1, len(lst)): v = lst
29
5115
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is: 65,535. But i'm trying to write a program to test this, assuming I didn't know this number in advance. I came up with the following but have two questions. Maybe someone can help? using System; using System.Collections.Generic; using System.Text;
2
3389
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. ';
3
7802
by: Michael Ekstrand | last post by:
I'm looking for a standard (or standard-ish) way to determine the maximum value representable by a size_t. I can't seem to find anything officially standard - cstddef doesn't seem to define such a thing, nor does climits. Applying grep to my /usr/include reveals an stdint.h header which defines a SIZE_MAX, some further research indicates that this is a C99 standard header but not standard for either C89 or standard C++. My primary...
2
2643
choke
by: choke | last post by:
I need to write a function in devc++ that creates an array of n integers, each element is equal to n*n+i*i-n*i where i is from 0 to n-1 as the array index. Within the same function I need to find the maximum value, minimum value and average of all elements. I'm stuck on the first one :/ I've practiced calling another function to find the maximum, and that seemed to work fine. I get a number in the millions when trying to do this all in one...
4
2853
Sandboxer
by: Sandboxer | last post by:
I want to be able to program Access to provide for me, by individual day, what my contract obligations are to my customers. Will Access recognize all the individual days in between a date range (simply a "from" date and a "to" date)? Additionally, I need to delivery a specific quantity of product when the customer's inventory is within about parameter levels. EXAMPLE: "During the Date Range January 1, 2010 through April 30, 2010 when...
18
4600
by: raylopez99 | last post by:
The maximum int for an array on my machine (a Pentium IV with 2 GB RAM) is < 330 Million...before you get an "out of memory" exception. I simply filled an array of this size with ints...I got as far as 320 M. So, myArray is a big as I can get. In theory a 32 bit int is 2.1 billion, but in practice, you cannot fill an array having anywhere near that number of elements. I suspect that it's because every element of an array takes up space...
0
9336
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
10111
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...
1
9902
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
9765
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...
1
7327
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
6603
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5215
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...
1
3866
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.