If it is a two's compliment machine the following shoud work
[code:1]int nminus1(int n)
{
return n+(~0);
}[/code:1]
when you flip all bits in 0 using ~ operator you get -1 (all bits are now 1), only in a two's compliment machine (pretty much all the machines out there :)). add that to n and you have n-1
Write a function that returns n-1 for an input of n
These will work only if the machine/os supports 2's complement arithmetic operations..
Write a function that returns n-1 for an input of n
it's pretty simple..
find the one's complement of n add 1 to it and again find the complement of the number, u will get n-1...
int value(int n)
{
int n1;
n1 = (~n) + 1 ;
return (~n1);
}
hope this is correct..
Write a function that returns n-1 for an input of n
If it is a two's compliment machine the following shoud work
[code:1]int nminus1(int n)
{
return n+(~0);
}[/code:1]
when you flip all bits in 0 using ~ operator you get -1 (all bits are now 1), only in a two's compliment machine (pretty much all the machines out there :)). add that to n and you have n-1