ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.