473,379 Members | 1,326 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,379 software developers and data experts.

comparison

I want to write something like
status_bit = (unsigned)(i - j) >31

as an equivalent code for
if(i < j){
{
status_bit = 1;
}else{
status_bit = 0;
}

This actually works for most of the cases but fails for two values
i = 0x80000000
j = 0x7fffffff

I want a linear code for the above i.e. without jumps, so
1. what could be the issue?
2. what could be the solution?

Oct 21 '07 #1
20 1576
On Oct 21, 3:10 pm, ndu...@gmail.com wrote:
I want to write something like
status_bit = (unsigned)(i - j) >31

as an equivalent code for
signed int i;
signed int j;
if(i < j){
{
status_bit = 1;

}else{
status_bit = 0;
}

This actually works for most of the cases but fails for two values
i = 0x80000000
j = 0x7fffffff

I want a linear code for the above i.e. without jumps, so
1. what could be the issue?
2. what could be the solution?

Oct 21 '07 #2
nd****@gmail.com wrote:
I want to write something like
status_bit = (unsigned)(i - j) >31

as an equivalent code for
if(i < j){
{
status_bit = 1;
}else{
status_bit = 0;
}
Try the simple
status_bit = (i < j);
This actually works for most of the cases but fails for two values
i = 0x80000000
j = 0x7fffffff

I want a linear code for the above i.e. without jumps, so
1. what could be the issue?
2. what could be the solution?
Is there a reason for avoiding the obvious
status_bit = (i < j);
?
Oct 21 '07 #3
"Martin Ambuhl" <ma*****@earthlink.neta écrit dans le message de news:
5o************@mid.individual.net...
nd****@gmail.com wrote:
>I want to write something like
status_bit = (unsigned)(i - j) >31

as an equivalent code for
if(i < j){
{
status_bit = 1;
}else{
status_bit = 0;
}

Try the simple
status_bit = (i < j);
>This actually works for most of the cases but fails for two values
i = 0x80000000
j = 0x7fffffff

I want a linear code for the above i.e. without jumps, so
1. what could be the issue?
2. what could be the solution?

Is there a reason for avoiding the obvious
status_bit = (i < j);
In other words: if there is a way to compute this without tests and jumps,
the compiler should know.

--
Chqrlie.
Oct 21 '07 #4
Is there a reason for avoiding the obvious
status_bit = (i < j);
I think that it is not guaranteed to get only 1 or 0 as a result of i
< j, this may result in 0 or non zero value is it not?

Oct 21 '07 #5
nd****@gmail.com wrote:
>
Is there a reason for avoiding the obvious
status_bit = (i < j);
I think that it is not guaranteed to get only 1 or 0 as a result of i
< j, this may result in 0 or non zero value is it not?
No, the relational operators yield either 1 or 0.

Oct 21 '07 #6
No, the relational operators yield either 1 or 0.
Is it guaranteed by ANSI C standard?
Oct 21 '07 #7
nd****@gmail.com wrote:
>
Is there a reason for avoiding the obvious
status_bit = (i < j);
I think that it is not guaranteed to get only 1 or 0 as a result of i
< j, this may result in 0 or non zero value is it not?
No.
(i < j) is can only be equal to either one or zero.

--
pete
Oct 21 '07 #8
nd****@gmail.com wrote:
>
>No, the relational operators yield either 1 or 0.
Is it guaranteed by ANSI C standard?
Yes. From n1256.pdf:

6.5.8 Relational operators

....

6 Each of the operators < (less than), (greater than), <= (less
than or equal to), and >= (greater than or equal to) shall yield 1
if the speci?ed relation is true and 0 if it is false.92)
The result has type int.

Oct 21 '07 #9
On Sun, 21 Oct 2007 10:10:51 +0000, ndun78 wrote:
I want to write something like
status_bit = (unsigned)(i - j) >31

as an equivalent code for
if(i < j){
{
status_bit = 1;
}else{
status_bit = 0;
}

This actually works for most of the cases but fails for two values
i = 0x80000000
j = 0x7fffffff

I want a linear code for the above i.e. without jumps, so
1. what could be the issue?
If *you* don't know that...
2. what could be the solution?
status_bit = (i < j);
Boolean operators evaluate to 0 if false and 1 if true.

--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Oct 21 '07 #10
On Oct 21, 3:10 pm, ndu...@gmail.com wrote:
I want to write something like
status_bit = (unsigned)(i - j) >31

as an equivalent code for
if(i < j){
{
status_bit = 1;

}else{
status_bit = 0;
}

This actually works for most of the cases but fails for two values
i = 0x80000000
j = 0x7fffffff

I want a linear code for the above i.e. without jumps, so
1. what could be the issue?
2. what could be the solution?
you as well write this as status_bit = i - j 1 ? 0 : 1

Oct 21 '07 #11
nd****@gmail.com wrote:
>>Is there a reason for avoiding the obvious
status_bit = (i < j);
I think that it is not guaranteed to get only 1 or 0 as a result of i
< j, this may result in 0 or non zero value is it not?
You are wrong. Check your elementary C textbook.
Oct 21 '07 #12
"abhy" <ab************@gmail.coma écrit dans le message de news:
11*********************@z24g2000prh.googlegroups.c om...
On Oct 21, 3:10 pm, ndu...@gmail.com wrote:
>I want to write something like
status_bit = (unsigned)(i - j) >31

as an equivalent code for
if(i < j){
{
status_bit = 1;

}else{
status_bit = 0;
}

This actually works for most of the cases but fails for two values
i = 0x80000000
j = 0x7fffffff

I want a linear code for the above i.e. without jumps, so
1. what could be the issue?
2. what could be the solution?

you as well write this as status_bit = i - j 1 ? 0 : 1
No, it does not work at all: the expression i - j 1 is not equivalent to
!(i < j) and even the more accurate i - j < 0 does not produce the correct
result if i - j overflows (as the OP correctly pointed out).

If you *really* want to compute the status bit with plain arithmetics then
you can use this:

status_bit = ((unsigned long long)((long long)i - j) >(CHAR_BIT *
sizeof(int) + 1)) & 1;

It should work if sizeof(long long) sizeof(int).
It may even be acceptably efficient on 64 bit architectures.

But it would be crazy to use such a convoluted expression when ``status_bit
= (i < j)'' is vastly more readable and quite unlikely to be a performance
killer.

--
Chqrlie

Oct 21 '07 #13
nd****@gmail.com writes:
I want to write something like
status_bit = (unsigned)(i - j) >31

as an equivalent code for
if(i < j){
You should buy a copy of the book _Hacker's Delight_ by Henry
S. Warren. It is a compendium of tricks like this. This
particular problem, and related inequalities, takes up a few
pages in chapter 2, for example.
--
Ben Pfaff
http://benpfaff.org
Oct 21 '07 #14
"Ben Pfaff" <bl*@cs.stanford.edua écrit dans le message de news:
87************@blp.benpfaff.org...
nd****@gmail.com writes:
>I want to write something like
status_bit = (unsigned)(i - j) >31

as an equivalent code for
if(i < j){

You should buy a copy of the book _Hacker's Delight_ by Henry
S. Warren. It is a compendium of tricks like this. This
particular problem, and related inequalities, takes up a few
pages in chapter 2, for example.
You will find this "Bit Twiddling Hacks" entertaining as well:

http://graphics.stanford.edu/~seander/bithacks.html

Good night!

--
Chqrlie.
Oct 21 '07 #15
"Ben Pfaff" <bl*@cs.stanford.edua écrit dans le message de news:
87************@blp.benpfaff.org...
nd****@gmail.com writes:
>I want to write something like
status_bit = (unsigned)(i - j) >31

as an equivalent code for
if(i < j){

You should buy a copy of the book _Hacker's Delight_ by Henry
S. Warren. It is a compendium of tricks like this. This
particular problem, and related inequalities, takes up a few
pages in chapter 2, for example.
You will find this "Bit Twiddling Hacks" entertaining as well:

http://graphics.stanford.edu/~seander/bithacks.html

Good night!

--
Chqrlie.

Oct 21 '07 #16
nd****@gmail.com wrote:
>
>>Is there a reason for avoiding the obvious
status_bit = (i < j);

I think that it is not guaranteed to get only 1 or 0 as a result
of i < j, this may result in 0 or non zero value is it not?
Yes, the result of a logical statement is guaranteed to be 0 or 1.

Please do not strip attributions (the "joe wrote:" headers) for
material you quote.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 22 '07 #17
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
>nd****@gmail.com wrote:
[...]
>>I think that it is not guaranteed to get only 1 or 0 as a result
of i < j, this may result in 0 or non zero value is it not?

Yes, the result of a logical statement is guaranteed to be 0 or 1.

By "logical statement", of course, you mean a logical *operator*.

Off the top of my head, the following operators yield logical results
that are guaranteed to be either 0 or 1:

= != < <= = ! && ||
Here don't you mean `==` instead of `=`?

<snip>

Oct 23 '07 #18
santosh <sa*********@gmail.comwrites:
Keith Thompson wrote:
[...]
>Off the top of my head, the following operators yield logical results
that are guaranteed to be either 0 or 1:

= != < <= = ! && ||

Here don't you mean `==` instead of `=`?
Yes, of course, thanks.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 24 '07 #19
On Tue, 23 Oct 2007 13:03:25 -0700, in comp.lang.c , Keith Thompson
<ks***@mib.orgwrote:
>
<OT>
A personal note: Don't expect a lot of postings from me in the next
few days. We've had to evacuate our home due to the huge fires here
in San Diego. We're ok (and so is our house so far), but my Internet
access will be sporadic until we can go back home.
</OT>
Hope everything is ok.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Oct 25 '07 #20
Mark McIntyre <ma**********@spamcop.netwrites:
On Tue, 23 Oct 2007 13:03:25 -0700, in comp.lang.c , Keith Thompson
<ks***@mib.orgwrote:
>>
<OT>
A personal note: Don't expect a lot of postings from me in the next
few days. We've had to evacuate our home due to the huge fires here
in San Diego. We're ok (and so is our house so far), but my Internet
access will be sporadic until we can go back home.
</OT>

Hope everything is ok.
Yes, thanks. We're back home now after staying at my mother's house
for two days. The air still stinks, but the worst is past, at least
for us (though not for people in other parts of the county where the
fires are still burning).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 25 '07 #21

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

Similar topics

0
by: bettervssremoting | last post by:
To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool This article makes a detailed comparison among SourceAnyWhere, SourceOffSite, VSS...
29
by: Steven D'Aprano | last post by:
Playing around with comparisons of functions (don't ask), I discovered an interesting bit of unintuitive behaviour: >>> (lambda y: y) < (lambda y: y) False Do the comparison again and things...
5
by: mayamorning123 | last post by:
A comparison among six VSS remote tools including SourceOffSite , SourceAnyWhere, VSS Connect, SourceXT, VSS Remoting, VSS.NET To view the full article, please visit...
46
by: yadurajj | last post by:
Hello i am newbie trying to learn C..I need to know about string comparisons in C, without using a library function,...recently I was asked this in an interview..I can write a small program but I...
0
by: bettervssremoting | last post by:
To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool This article makes a detailed comparison among SourceAnyWhere, SourceOffSite, VSS...
37
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will raise a TypeError instead of the current...
7
by: bcutting | last post by:
I am looking for a way to take a large number of images and find matches among them. These images may not be exact replicas. Images may have been resized, cropped, faded, color corrected, etc. ...
0
by: SvenMathijssen | last post by:
Hi, I've been wrestling with a problem for some time that ought to be fairly simple, but turns out to be very difficult for me to solve. Maybe someone here knows the answer. What I try to do is...
1
by: Lars B | last post by:
Hey guys, I have written a C++ program that passes data from a file to an FPGA board and back again using software and DMA buffers. In my program I need to compare the size of a given file against...
11
by: Andrus | last post by:
I created dynamic extension methods for <= and < SQL comparison operators: public static IQueryable<TLessThanOrEqual<T>(this IQueryable<Tsource, string property, object value); public static...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.