If number < 0 return zero else return one

L

Lakshmi Sreekanth

Hi,

My problem: I need to return 0 if 'number < 0' other wise return
value
should be 1.

Condition: Only by using below operators, We should not use any loops
or if or switch or any thing

Legal ops: ! ~ & ^ | + << >>

Ex: if (number < 0) // But, v should not use "if"
condition.
return 0;
return 1;


Waiting for clue.

Regards,
Laks ..... Sree.....
 
M

Mark Bluemel

Hi,

My problem: I need to return 0 if 'number  < 0' other wise return
value
should be 1.

Condition: Only by using below operators, We should not use any loops
or if or switch or any thing

Legal ops: ! ~ & ^ | + << >>

And later, we have to move a pea across a pool of treacle only using
our noses.
 
D

David RF

Hi,

My problem: I need to return 0 if 'number  < 0' other wise return
value
should be 1.

Condition: Only by using below operators, We should not use any loops
or if or switch or any thing

Legal ops: ! ~ & ^ | + << >>

Ex:           if  (number < 0)        // But, v should not use "if"
condition.
                       return 0;
                return 1;

Waiting for clue.

Take a look
http://graphics.stanford.edu/~seander/bithacks.html#CopyIntegerSign
 
K

Keith Thompson

Lakshmi Sreekanth said:
My problem: I need to return 0 if 'number < 0' other wise return
value
should be 1.

Condition: Only by using below operators, We should not use any loops
or if or switch or any thing

Legal ops: ! ~ & ^ | + << >>

Ex: if (number < 0) // But, v should not use "if"
condition.
return 0;
return 1;


Waiting for clue.

Here's a clue: do your own homework.
 
M

Mug

Hi,

My problem: I need to return 0 if 'number  < 0' other wise return
value
should be 1.

Condition: Only by using below operators, We should not use any loops
or if or switch or any thing

Legal ops: ! ~ & ^ | + << >>

Ex:           if  (number < 0)        // But, v should not use "if"
condition.
                       return 0;
                return 1;

Waiting for clue.

Regards,
Laks ..... Sree.....

#include <stdio.h>
int main(int argc, const char *argv[])
{
int n;
for (n = -2; n < 2; n++) {
printf("value %d answer :%d\n",n,!(0xf0000000&n));
}
return 0;
}

no explication needed i think
 
M

Mug

My problem: I need to return 0 if 'number  < 0' other wise return
value
should be 1.
Condition: Only by using below operators, We should not use any loops
or if or switch or any thing
Legal ops: ! ~ & ^ | + << >>
Ex:           if  (number < 0)        // But, v should not use "if"
condition.
                       return 0;
                return 1;
Waiting for clue.
Regards,
Laks ..... Sree.....

#include <stdio.h>
int main(int argc, const char *argv[])
{
        int n;
        for (n = -2; n < 2; n++) {
                printf("value %d answer :%d\n",n,!(0xf0000000&n));
        }
        return 0;

}

no explication needed i think

well i think i need to give a small explication:
the range of int is between 0x0 to 0xffffffff
the 0xf??????? are negative integer, the 0x0??????? are positive
integer
so when we do integer&0xf0000000 make all positive number and 0 be 0,
and !0 =1
and all negative number will stay be 0xf0000000 which is -1 and !-1
equal to 1.
(note the loop here is not count in the problem unit,it's just for
test value from -2 to 1)
 
J

jameskuyper

Mug said:
Hi,

My problem: I need to return 0 if 'number < 0' other wise return
value
should be 1.

Condition: Only by using below operators, We should not use any loops
or if or switch or any thing

Legal ops: ! ~ & ^ | + << >>

Ex: if (number < 0) // But, v should not use "if"
condition.
return 0;
return 1;

Waiting for clue.

Regards,
Laks ..... Sree.....

#include <stdio.h>
int main(int argc, const char *argv[])
{
int n;
for (n = -2; n < 2; n++) {
printf("value %d answer :%d\n",n,!(0xf0000000&n));
}
return 0;
}

no explication needed i think

Perhaps a little. Could you explain how your test works in each of the
following cases?

n == 0x10000000
n == -0x10000000
INT_MAX < 0x7FFFFFFF
INT_MAX > 0x7FFFFFFF
INT_MIN == - INT_MAX
 
M

Mug

#include <stdio.h>
int main(int argc, const char *argv[])
{
   int n;
   for (n = -2; n < 2; n++) {
           printf("value %d answer :%d\n",n,!(0xf0000000&n));
   }
   return 0;
}
no explication needed i think

Perhaps a little. Could you explain how your test works in each of the
following cases?

    n == 0x10000000
    n == -0x10000000
you are right i did forget that f is is (1111) in binary , it should
be (1000) so 8, it should rather be 0x80000000
    INT_MAX < 0x7FFFFFFF
    INT_MAX > 0x7FFFFFFF
if you modify the value of INT_MAX or INT_MIN then i have no idea how
to deal with.
 
J

jameskuyper

Mug said:
Mug said:
On Sep 14, 12:14 pm, Lakshmi Sreekanth <[email protected]>
wrote:
Hi,
My problem: I need to return 0 if 'number < 0' other wise return
value
should be 1.
Condition: Only by using below operators, We should not use any loops
or if or switch or any thing
Legal ops: ! ~ & ^ | + << >>
Ex: if (number < 0) // But, v should not use "if"
condition.
return 0;
return 1;
Waiting for clue.
Regards,
Laks ..... Sree.....
#include <stdio.h>
int main(int argc, const char *argv[])
{
int n;
for (n = -2; n < 2; n++) {
printf("value %d answer :%d\n",n,!(0xf0000000&n));
}
return 0;
}
no explication needed i think

Perhaps a little. Could you explain how your test works in each of the
following cases?

n == 0x10000000
n == -0x10000000
you are right i did forget that f is is (1111) in binary , it should
be (1000) so 8, it should rather be 0x80000000
INT_MAX < 0x7FFFFFFF
INT_MAX > 0x7FFFFFFF
if you modify the value of INT_MAX or INT_MIN then i have no idea how
to deal with.

It's not a question of modifying those values. They are set by the C
implementation in <limits.h>. The important thing is that C
implementations are not required to set them to the values you seem to
expect them to have.
 
S

Seebs

My problem: I need to return 0 if 'number < 0' other wise return
value
should be 1.
Okay.

Condition: Only by using below operators, We should not use any loops
or if or switch or any thing

Legal ops: ! ~ & ^ | + << >>

Ex: if (number < 0) // But, v should not use "if"
condition.
return 0;
return 1;


Waiting for clue.

Regards,
Laks ..... Sree.....

Sounds like a homework problem.

This is actually moderately hard to do portably; if you're allowed to assume
that you know the target architecture, you may be able to do it by something
as trivial as masking out a sign bit. (But be wary; there probably exists
a machine where there's a -0 integer value, which would trip you up.)

-s
 
J

jameskuyper

Mug wrote:
....
the range of int is between 0x0 to 0xffffffff

The range of int is INT_MIN to INT_MAX, and INT_MIN can be as small as
-32767, and there's no upper limit on how big it can be. INT_MAX can
be as small as 32767, and there's no upper limit on how high it can
be.
 
M

Mug

Mug said:
Mug wrote:
On Sep 14, 12:14 pm, Lakshmi Sreekanth <[email protected]>
wrote:
Hi,
My problem: I need to return 0 if 'number  < 0' other wise return
value
should be 1.
Condition: Only by using below operators, We should not use any loops
or if or switch or any thing
Legal ops: ! ~ & ^ | + << >>
Ex:           if  (number < 0)        // But, v should not use "if"
condition.
                       return 0;
                return 1;
Waiting for clue.
Regards,
Laks ..... Sree.....
#include <stdio.h>
int main(int argc, const char *argv[])
{
   int n;
   for (n = -2; n < 2; n++) {
           printf("value %d answer :%d\n",n,!(0xf0000000&n));
   }
   return 0;
}
no explication needed i think
Perhaps a little. Could you explain how your test works in each of the
following cases?
    n == 0x10000000
    n == -0x10000000
you are right i did forget that f is is (1111) in binary , it should
be (1000) so 8, it should rather be 0x80000000
    INT_MAX < 0x7FFFFFFF
    INT_MAX > 0x7FFFFFFF
if you modify the value of INT_MAX or INT_MIN then i have no idea how
to deal with.

It's not a question of modifying those values. They are set by the C
implementation in <limits.h>. The important thing is that C
implementations are not required to set them to the values you seem to
expect them to have.

i check out limits.h on wikipedia, if i do well understand what u
mean, different system might have there own
implementation of limits.h, so INT_MAX may not be be 0x7fffffff, okay
we suppose the INT_MIN should be -(INT_MAX +1)(why the hell people
need to define the things non standard way?),so the value become
n&INT_MIN, but as u noticed if INT_MIN==-INT_MAX, it will not work.
i just take the question in a general way, if the system define
INT_MIN other than -(INT_MAX+1), in my opinion we need to do another
program to resolve the same question.
 
J

jameskuyper

Mug wrote:
....
... (why the hell people
need to define the things non standard way?)

Because there is in fact no applicable standard violated by such
choices. Because ways of representing integers that are different from
the way that you're used to are, on some machines, more convenient or
efficient. In particular, the ideal size for an int differs a great
deal from one machine to another.

16 and 32 bit ints are the most common, 64 bits is fairly popular, and
there have been various oddball machines where a size that wasn't a
power of two was most convenient. Some machines have a word size of 36
bits, for instance, which makes a power-of-two sizes rather
inefficient. On some systems that I've heard about, there was no
hardware support for large integer types, so they were emulated by
storing them in the mantissa of a floating point type.
 
S

Seebs

i check out limits.h on wikipedia, if i do well understand what u
mean, different system might have there own
implementation of limits.h, so INT_MAX may not be be 0x7fffffff, okay
we suppose the INT_MIN should be -(INT_MAX +1)

Not necessarily. That's a common implementation, but it's not the only one.
(why the hell people need to define the things non standard way?)

It's not non-standard, it's just different from what you're used to.
,so the value become
n&INT_MIN, but as u noticed if INT_MIN==-INT_MAX, it will not work.
i just take the question in a general way, if the system define
INT_MIN other than -(INT_MAX+1), in my opinion we need to do another
program to resolve the same question.

Probably, yes.

-s
 
N

Nick Keighley

On Sep 14, 12:14 pm, Lakshmi Sreekanth <[email protected]>
wrote:
#include <stdio.h>
int main(int argc, const char *argv[])
{
        int n;
        for (n = -2; n < 2; n++) {
                printf("value %d answer :%d\n",n,!(0xf0000000&n));
        }
        return 0;

no explication needed i think

well i think i need to give a small explication:
the range of int is between 0x0 to 0xffffffff
the 0xf??????? are negative integer, the 0x0??????? are positive
integer

and all the other numbers with the top bit set?
so when we do integer&0xf0000000 make all positive number and 0 be 0,
and !0 =1

if this is supposed to make things clearer...
and all negative number will stay be 0xf0000000 which is -1
what?

and !-1 equal to 1.

not on my computer
(note the loop here is not count in the problem unit,it's just for
test value from -2 to 1

what?
 
L

Lakshmi Sreekanth

Hi,

Finally i got the solution.

int sign(int number)
{

return ( number >> 31) + 1 ; //For 32-bit
}

If number < 0 the function return zero other wise it return one!!!

Thanks.
 
J

jameskuyper

Lakshmi said:
Hi,

Finally i got the solution.

int sign(int number)
{

return ( number >> 31) + 1 ; //For 32-bit

If 'number' has a negative value, the value returned by the expression
(number >> 31) is defined by the implementation, not by the C
standard. Therefore, whether or not that result allows you to
distinguish negative values from positive ones depends upon which
implementation of C you use.
 
D

David RF

Hi,

Finally i got the solution.

int sign(int number)
{

   return  ( number >>  31) + 1 ;           //For 32-bit

}

If number < 0 the function return zero other wise it return one!!!

Thanks.

Finally?


Take a lookhttp://graphics.stanford.edu/~seander/bithacks.html#CopyIntegerSign

24 hours is too much for transform this:

v >> (sizeof(int) * CHAR_BIT - 1);

into this:

( number >> 31) + 1

Flunked!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top