-1

I am trying to print how the negative number(decimal eg. -7 , -8 ) is stored in computer memory in binary form , But not getting desired output. Please rectify my mistakes..

#include<iostream>
#include<math.h>
using namespace std;

int main(){

   // Enter the negative number
    int n;
    cin>>n;

    if(n<0){
      n = -n;
     }

  // Find the negation of the number
  int m = ~n;

  int bit;

  long int ans=0;

  int i=0;

  while(m!=0){
    bit = m & 1;
    ans = (bit * pow(10,i)) + ans;

    m = m>>1;
    i++;
    }

   ans+=1;

  cout<<ans;


   }
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • what is input ? what is desired output? – 463035818_is_not_an_ai Jun 14 '23 at 07:48
  • 4
    Please don't use the floating point `pow` function for integer powers. – Some programmer dude Jun 14 '23 at 07:51
  • As for your problem, just print bit by bit instead of collecting all bits as a decimal value (which will be *much* to large for a 32-bit integer to handle, and remember there are systems where `long` is still 32 bits). – Some programmer dude Jun 14 '23 at 07:53
  • Lastly, almost all systems in use today are using [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) for handling negative numbers. It's even mandatory since the C++20 standard IIRC. So except as a school or similar assignment, there's really no need to check how your system stores negative numbers, it's well-documented. – Some programmer dude Jun 14 '23 at 07:56

1 Answers1

0

You have two issues: m should be an unsigned int instead of a signed int. The problem with signed int is, you put a positive int into the negation, so m will always be a negative signed number going into the loop. However, because of sign extension on shift your loop will not be able to reach its terminating condition anymore and your program will just hang forever.

The second issue is that your variable ans is not big enough to store the decimal representation of the bit pattern you are trying to print. It will overflow halfway through the loop which is undefined behavior and will most likely print a garbage result. As was suggested in the comments, the easiest way to avoid this is to just print out the individual bits inside the loop.

ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
  • I am not getting it , can you please explain me with the help of code – learner2604 Jun 14 '23 at 18:14
  • @learner2604 Can you elaborate on what in particular you have trouble understanding? – ComicSansMS Jun 14 '23 at 19:12
  • I am printing the bits but it is printing in reversing order . So do i have to make another variable (mask) to print the bits in a right order . But I don't know how to make this mask variable and hence required code of this – learner2604 Jun 15 '23 at 03:30
  • @learner2604 You never mentioned in your question what your expected output is, so I only fixed the obvious issues that prevented your program from running. If you need help fixing your program logic as well, you will need to add more details what you actually expect it to do. From what you have written so far, it is very difficult to tell what you are trying to do here. – ComicSansMS Jun 15 '23 at 03:47
  • I wanted to print how negative decimal numbers are stored in memory . As we know that the negative number is stored by taking the 2's compliment of the entered negative number . Therefore i want to print that 2's compliment and required correct logic to do so. – learner2604 Jun 15 '23 at 11:48