negation for integer

11Sep08

I just found a simple piece of code in my mail archive, i had sent in 2006.

//function performs the negation for integer.

public int getNegate(int n)
{
  string strBin = ConvertToBin(n);
  string strNegation = "";
  int c,count,i;
  int nDec;

  if (strBin.Length != 8 )
    for(i=strBin.Length; i<8; i++)
      strBin = "0" + strBin;

  count = 1;
  while (count <= strBin.Length)
  {
    c = Convert.ToInt32(strBin.Substring(count-1,1));
    if (c == 1)
      strNegation = strNegation + "0";
    else
      strNegation = strNegation + "1";
    count = count + 1;
  }
  nDec = ConvertDecimal(strNegation);
  return nDec;
}

0 Antworten to “negation for integer”


  1. Keine Kommentare

Kommentieren