LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-06-2008, 12:23 AM   #1
dedexes
LQ Newbie
 
Registered: Jul 2007
Posts: 21

Rep: Reputation: 16
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
}

Last edited by dedexes; 03-06-2008 at 12:28 AM.
 
Old 03-06-2008, 01:53 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Do not expect LQ members to do your homework - you will learn much more by doing it yourself.
 
Old 03-06-2008, 03:11 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I can't say that I know how to do that but you can have a look and see what NOT gives you.
 
Old 03-06-2008, 08:57 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

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

Last edited by Hko; 03-06-2008 at 09:29 AM.
 
Old 03-06-2008, 10:19 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 03-06-2008, 12:12 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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

Last edited by ta0kira; 03-06-2008 at 12:15 PM.
 
Old 03-06-2008, 04:30 PM   #7
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Rep: Reputation: 15
Quote:
Originally Posted by dedexes View Post
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.

Last edited by Cyhaxor; 03-06-2008 at 04:37 PM.
 
Old 03-06-2008, 04:41 PM   #8
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
...but those aren't bit operators.
 
Old 03-06-2008, 06:25 PM   #9
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
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
 
Old 03-06-2008, 07:50 PM   #10
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Rep: Reputation: 15
Quote:
Originally Posted by graemef View Post
...but those aren't bit operators.
Sorry! My bad..
 
Old 06-28-2008, 06:17 AM   #11
dedexes
LQ Newbie
 
Registered: Jul 2007
Posts: 21

Original Poster
Rep: Reputation: 16
#include <stdio.h>
int main()
{
int x;
scanf("%d",&x);
x=~(x-1);
printf("%d\n",x);
return 0;
}
 
Old 06-28-2008, 08:23 AM   #12
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by dedexes View Post
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.
 
Old 06-29-2008, 12:49 AM   #13
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
But +1 isn't a bit operation
 
Old 06-30-2008, 01:10 AM   #14
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by Dan04 View Post
But +1 isn't a bit operation
Can you prove it? And what exactly do you call "bit operation"?
 
Old 06-30-2008, 02:09 AM   #15
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by ErV View Post
Can you prove it? And what exactly do you call "bit operation"?
Possibly something that operates at the bit rather than byte level?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration