Saturday, 1 April 2023

Demistifying the enigma of negative numbers in programming

I came across this linkedin blog [How are negative numbers stored in binary?] [How the negative numbers are stored in memory? ] . These blogs provoked a related question : Why are 2's complement necessary at first place and why such systems are also not used for the quotidian Decimal system ? 

Negative numbers has alwyas been difficult to undertand reason being its vicarious nature - when your friend lends you green dollars you can touch it, feel it, grasp it, but when he asks back you cannot really "see" any money, unless you press hard on your memory. The negative number are particularly recondite when it comes to how computer deals with this "negativity". 

Let's revisit DECIMAL SYSTEM :

* only symbols, we call digits, permitted are : 0 to 9 (i.e. Ten symbols) 

* we can represent all WHOLE Numbers (0,1,2,3...∞) using only these 10 symbols and their infinite jumbling combinations. (read Permutaions & Combinations). 

At this point, Primates had no idea about what "-54" meant ? They logged informations like "The bald Primate owes 50 rice grains to the hirsute Primate ". But as much water has flowed under the bridge some primate suggested why not replace "owed" with yet another symbol with, say, "-".

* Therefore, inorder to represent INTEGERS (-∞,...-3,-2,-1,0,1,2,3 ...) we agreed to use an additional symbol "-"




So, the Decimal system(0-9) is easy because we have made it easier by borrowing extra symbol of (-)ve.  

Before we strike the pith of  the blog title, bear me to answer this qustion : 

How could we have written/represented negative numbers if you were not permitted to use an extra symbol like "-"

Let me rephrase question : "How will you write -7 without using (-) symbol in decimal system?" [Constraint: you can use any digit (0-9) for maximum 4 times as in 8764]

(why I have limited to 4 digits in this excercise? Because unlike when wrting on paper, in computer the size is limited based on data type - for example, an integer is stored in 32 bits)

Propsed Solution

We have to find B such that A + B = 0 

A = 7 = 0007 (represented as 4 digit) 

Step I : now take 9's complement* = 9999-0007 = 9992 [DEFINITION: 9's complement of A = highest number possible - A]

Step II: Add 1 : 9993 (This is B i.e. -7) [10's complement of A]

Lets verify : A + B = 0007 + 9993 = 1 0000 = 0000 (discard the leading 1 because we can only use 4 digits)

Agreeing to Agree on somethings is at the cornerstone of any system we devise to ease our transactions. What we have implictly agreed above is that if the left most symbol will be of highest value out of 10 symbols(i.e. 9) then it will represent negative number. And if the left most symbol is of the lowest value out of 10 symbols (i.e 0) then it will represent positive number. 

Then obvious counter question will be : how can we represent a number greater than 8999 , say 9000 using above agreement? Won't this 9000 be actually representing : positive 1000 

Let A = 1000

 I. 9999 - 1000 = 8999 (9's complement of 1000)

II. 8999 + 1    = 9000 (10's complement of 1000)

The answer is no, 9000 = -1000. Because for 4 spaces we cannot represent (positive) number greater than 8999. Why? Because of our prior tacit agreement about TRADEOFF between RANGE & SIGN. 

either, you can represent 10 thousand WHOLE Numbers from 0000 to 9999

or, you can represent 999 negative integers(9001to999) & 999 positive integers(0001to0999) and one zero (9000 or 0000). Total numbers = 1999 The leading number will act as sign indicator. Such drastic fall in range because one space, the leftmost space, which could have hold 10 information is used to present only two infromation (+/-). 


Now, Enter the world of BINARY SYSTEM : 

* only two symbols: 0 and 1 are permitted.

In the Binary system, at the machine level we dont have luxury of adding (-ve) symbol. Everything is black(0) & white(1) - either 0 or 1. (unless you are using quantum computer). 

So early programmers have devised an igenious way(already explained above) to overcome this scarcity. 

A = 7 is represented in binary (8-bit) as  0000 1101

Step I : 1's complement of A : (highest no possible - A) = 1111 1111 - 0000 1101 = 1111 0010

Step II: 2's complement of A :   1's complement + 1 = 1111 0010 +1 = 1111 0011

Verify : 0000 1101 + 1111 0011 =  1 0000 0000 = 0000 0000 (leading 1 will be discarded for 8-bit)


PS: the range loss in binary system is much lesser compared to decimal system . Hence it made sense to use above representation in binary systems. 

Capacity reducation for 4 spaces if we store negative numbers : 

1. Decimal system : 10,000 --> 2000 (1000 postive , 1000 negative) : 5x loss

2. Binary system :  16 (0000 to 1111) --> 15(1111/-7 to 0 to 0111/+7) - hardly any loss

Thanks. 

Thursday, 9 March 2023

Google Sheet + Apps Script : Day03 (Removes blank Cells in a column consisting of hyperlinked strings)

 

Turns a spaced column into a compressed one :

many blank cells in between ==>blank cols removed

function removeEmptyCellsInColumn() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];

    var start=130;
    var last=300;

    var i=start;
    var j=i+1;

    var rangeCut,rangePaste;
    var a_i;
    var a_j =sheet.getRange(j,2).getValue()

    while(j<last){
        if(a_j !== "") {// if a[j] isNOTempty //a[i+1] =a[j];
          rangePaste = sheet.getRange(i+1,2); //pulls up next val in  next contiguous empty col
          rangeCut =sheet.getRange(j,2);
          a_i =sheet.getRange(i,2).getValue();
          
          var richValue = SpreadsheetApp.newRichTextValue()
                        .setText(a_j)
                        .setLinkUrl(sheet.getRange(j,2).getRichTextValue().getLinkUrl())
                        .build();
          rangePaste.setRichTextValue(richValue); //paste at a[i]
          i++;
          rangeCut.clear();//clears or cuts the a[j]
        }
        j++; 
        a_j =sheet.getRange(j,2).getValue();
      }
}

Google Sheet + Apps Script : Day02(Removes/Blanks Cell containing Digit/Number)

 

This code helps remove all numbers in 3rd column of third sheet in a SpreadSheet.


function numberRemoverFromColumn() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[2];
  //  var sheet = ss.getSheetByName("SheetName"); 
 
 var row=1;
  var startRowAt=2; //starting row
  var endRowAt=2192;//last row

  for ( row=startRowAtrow<endRowAtrow++ ) {
    var range = sheet.getRange(row,3);
    var presentCellValue =range.getValues(); //=values[0][0];
      if(presentCellValue%2===0 || presentCellValue%2===1){
        Logger.log(presentCellValue+" will be blanked now");
        var richValue = SpreadsheetApp.newRichTextValue()
                        .setText("")
                        .build();
        range.setRichTextValue(richValue)
      }
  }
}



Google Sheet + Apps Script : Day1

Created a program to change each strings containing cells of first column into hyperlinks redirecting to itself : 

function selfRefrencingHyperlinkMaker() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[1];
  //  var sheet = ss.getSheetByName("SheetName"); 
  var row;
  var startRowAt=2;
  var endRowAt=150;
  for ( row =startRowAtrow<endRowAtrow++ ) {
    var range = sheet.getRange(row,1);
    var values = range.getValues();
      if(values[0][0].length >0){
        var richValue = SpreadsheetApp.newRichTextValue()
                        .setText(values)
                        .setLinkUrl("#gid="+sheet.getSheetId()+"&range=A"+row)
                        .build();
        range.setRichTextValue(richValue)
      }
  }
}

Saturday, 28 January 2017

How to clean up workspace after compiling and running C/C++ programs in windows

Hi Cpp Programmers,

If you are tired of removing the *.o and *.exe files generated after compiling and running c/cpp files then you have come to right place.

This blog explains how to remove such files in Windows Platform. Later script for Linux version will also be added.

Suppose you have a parent directory called MyCppCodeRepo within which you might have children directory e.g. LinkedList, Trees, etc.

Here are the steps :


  1. Go to MyCppCodeRepo e.g. D:/myfile/MyCppCodeRepo/
  2. Create a file cleanUp.txt.
  3. Paste the following code : del /S *.o *.exe
  4. (OPTIONAL) If you want to see the logs (i.e which files are being deleted) add an additional line pause .
  5. Save and Exit.
  6. Rename the file to cleanUp.bat . This transforms the text file to a batch file.
  7. Now whenever you want to delete files of type *.o and *.exe in the current directory or any nested folder just double click it.




Explanation : del : deletes file supplied to it.
                      /S   : looks for nested folder (recursively)
                     *.o and *.exe : * is for file name of any length. .o and .exe file types which we want to delete 


You can copy and paste this small batch file in any directory and remove the desired file types. Suppose, after few months you start Java Programming then compiling Java files (*.java) generates java bytecodes which have *.class extension. If you want to delete these class files then just append *.class in line 3 of above steps.

Git provide .gitignore for this specific purpose - to exclude such binaries and other files generated at compile time. But writing your scripts to achieve these small tasks has its own satisfaction. Hope it helps you in keeping your work-space clean.

HAPPY PROGRAMMING.