|
|
|
#17 |
|
Messages: n/a
Hébergeur: |
"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. |
|
|
|
#18 |
|
Messages: n/a
Hébergeur: |
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" |
|
|
|
#19 |
|
Messages: n/a
Hébergeur: |
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> |
|
|
|
#20 |
|
Messages: n/a
Hébergeur: |
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" |
|
|
|
#21 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#22 |
|
Messages: n/a
Hébergeur: |
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" |
|
![]() |
| Outils de la discussion | |
|
|