PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > comparison
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comparison

Réponse
 
LinkBack Outils de la discussion
Vieux 21/10/2007, 11h10   #1 (permalink)
ndun78@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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?

  Réponse avec citation
Vieux 21/10/2007, 11h20   #2 (permalink)
ndun78@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: comparison

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?



  Réponse avec citation
Vieux 21/10/2007, 11h20   #3 (permalink)
Martin Ambuhl
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: comparison

ndun78@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);
?


  Réponse avec citation
Vieux 21/10/2007, 11h46   #4 (permalink)
Charlie Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: comparison

"Martin Ambuhl" <mambuhl@earthlink.net> a écrit dans le message de news:
5o0ngiFkk1juU1@mid.individual.net...
> ndun78@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.


  Réponse avec citation
Vieux 21/10/2007, 11h56   #5 (permalink)
ndun78@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: comparison


> > 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?

  Réponse avec citation
Vieux 21/10/2007, 12h11   #6 (permalink)
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: comparison

ndun78@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.

  Réponse avec citation
Vieux 21/10/2007, 12h14   #7 (permalink)
ndun78@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: comparison


> No, the relational operators yield either 1 or 0.

Is it guaranteed by ANSI C standard?


  Réponse avec citation
Vieux 21/10/2007, 12h20   #8 (permalink)
pete
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: comparison

ndun78@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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h52.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11211 seconds with 16 queries