LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   2 to -2 in c (https://www.linuxquestions.org/questions/programming-9/2-to-2-in-c-626057/)

dedexes 03-06-2008 12:23 AM

2 to -2 in c
 
how to invert 2 to -2 with bit operation in c programming language.

main(){
int x=2;
int y;
y= ....
/* bit operations */
........
printf("%d",y); // -2
}

jlliagre 03-06-2008 01:53 AM

Do not expect LQ members to do your homework - you will learn much more by doing it yourself.

graemef 03-06-2008 03:11 AM

I can't say that I know how to do that but you can have a look and see what NOT gives you.

Hko 03-06-2008 08:57 AM

http://en.wikipedia.org/wiki/Two's_complement
and:
http://www.cprogramming.com/tutorial...operators.html

theNbomr 03-06-2008 10:19 AM

To understand the problem, I suggest printing the numbers of interest in binary format. From that you will be able to see the patterns which should reveal the boolean logic required to achieve the transformation. As Hko points out, you should also understand the nature of two's complement integer format and related arithmetic. A book or online tutorial in assembler language may be a useful resource for learning this subject.

--- rod.

ta0kira 03-06-2008 12:12 PM

There's no way to do it purely with bit operations unless you make assumptions about the system you're working on. int isn't always signed, either, so you need signed int to be sure. Unless you just assume what bit is the sign bit, you need to use a different signed/unsigned pair to derive which bit it actually is. If you can infer what I mean it will be a very easy task, though a portable solution is just a long way of saying * -1 or | -0.
ta0kira

Cyhaxor 03-06-2008 04:30 PM

Quote:

Originally Posted by dedexes (Post 3079668)
how to invert 2 to -2 with bit operation in c programming language.

main(){
int x=2;
int y;
y= ....
/* bit operations */
........
printf("%d",y); // -2
}

Assuming that you are not using an UNSIGNED integer (int)...

Code:

y = x*-1;
It isn't so hard if think a little bit about it..

Optional
Also if you want to check the number whether it's negative or positive before convert it you can make an if statement.
Code:

if (x>0)
{
  y=x*-1;
}

This will be helpful if you want to convert only positive numbers into negative. If you omit the if statement then the program will convert each number into the opposite one.

graemef 03-06-2008 04:41 PM

...but those aren't bit operators.

exvor 03-06-2008 06:25 PM

HKO's wiki link tells you how to do exactly this via bit operations. It however does not show you the code on how to do it so you will need to write that yourself.

Hell it even gives you the logic just gotta fill in the code bits :P

Cyhaxor 03-06-2008 07:50 PM

Quote:

Originally Posted by graemef (Post 3080453)
...but those aren't bit operators.

Sorry! My bad..

dedexes 06-28-2008 06:17 AM

#include <stdio.h>
int main()
{
int x;
scanf("%d",&x);
x=~(x-1);
printf("%d\n",x);
return 0;
}

ErV 06-28-2008 08:23 AM

Quote:

Originally Posted by dedexes (Post 3079668)
how to invert 2 to -2 with bit operation in c programming language.

main(){
int x=2;
int y;
y= ....
/* bit operations */
........
printf("%d",y); // -2
}

Code:

int y = (int)(~((unsigned int)x))+1;
You could guess it yourself.

Dan04 06-29-2008 12:49 AM

But +1 isn't a bit operation ;)

ErV 06-30-2008 01:10 AM

Quote:

Originally Posted by Dan04 (Post 3198228)
But +1 isn't a bit operation ;)

Can you prove it? And what exactly do you call "bit operation"?

graemef 06-30-2008 02:09 AM

Quote:

Originally Posted by ErV (Post 3198974)
Can you prove it? And what exactly do you call "bit operation"?

Possibly something that operates at the bit rather than byte level?


All times are GMT -5. The time now is 07:25 AM.