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, 22h57   #17
Charlie Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: comparison

"Ben Pfaff" <blp@cs.stanford.edu> a écrit dans le message de news:
87lk9wm9d2.fsf@blp.benpfaff.org...
> ndun78@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.



  Réponse avec citation
Vieux 23/10/2007, 21h03   #18
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: comparison

CBFalconer <cbfalconer@yahoo.com> writes:
> ndun78@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:

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

Statements and operators that interpret an expression as a boolean
value treat 0 as false, any non-zero value as true. This applies to
if, while, do-while, the second expression in a for statement, !, &&,
||, ?:.

A *function* that returns a logical boolean result should not be
assumed to return just 0 or 1 unless it's specifically documented to
do so. For example, the is*() functions declared in <ctype.h> return
0 for false, but can return any arbitrary non-0 value for true.

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

--
Keith Thompson (The_Other_Keith) kst-u@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"
  Réponse avec citation
Vieux 23/10/2007, 21h08   #19
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: comparison

Keith Thompson wrote:

> CBFalconer <cbfalconer@yahoo.com> writes:
>> ndun78@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>

  Réponse avec citation
Vieux 24/10/2007, 08h10   #20
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: comparison

santosh <santosh.k83@gmail.com> writes:
> 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) kst-u@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"
  Réponse avec citation
Vieux 25/10/2007, 23h45   #21
Mark McIntyre
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: comparison

On Tue, 23 Oct 2007 13:03:25 -0700, in comp.lang.c , Keith Thompson
<kst-u@mib.org> wrote:

>
><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
  Réponse avec citation
Vieux 26/10/2007, 00h41   #22
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [OT] comparison

Mark McIntyre <markmcintyre@spamcop.net> writes:
> On Tue, 23 Oct 2007 13:03:25 -0700, in comp.lang.c , Keith Thompson
> <kst-u@mib.org> wrote:
>
>>
>><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) kst-u@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"
  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 12h59.


Édité par : vBulletin® version 3.7.3
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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20243 seconds with 14 queries