|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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); ? |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
"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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
> > 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? |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
> No, the relational operators yield either 1 or 0. Is it guaranteed by ANSI C standard? |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|