Tuesday, September 25, 2012

Now and Then...

  1. Pseudocode is a programming language that never complains about syntax errors.
  2. As long as you are rigorous and precise, you can be as sloppy as you want.
  3. Quintus: "People should know when they are conquered." Maximus: "Would you, Quintus? Would I?"
  4. Do not mistake the pointing finger for the moon.
  5. Say what you mean, mean what you say.
  6. Gregory (Scotland Yard detective): "Is there any other point to which you would wish to draw my attention?" Holmes: "To the curious incident of the dog in the night-time." Gregory: "The dog did nothing in the night-time." Holmes: "That was the curious incident."
  7. EXERCISES 4. [HM45] Prove that when n is an integer, n > 2, the equation x^n + y^n = z^n has no solution in positive integers x, y, z.
  8. A poet once said, "The whole universe is in a glass of wine." We will probably never know in what sense he meant that, for poets do not write to be understood. But it is true that if we look at a glass of wine closely enough we see the entire universe. There are the things of physics: the twisting liquid which evaporates depending on the wind and weather, the reflections in the glass, and our imagination adds the atoms. The glass is a distillation of the earth's rocks, and in its composition we see the secrets of the universe's age, and the evolution of stars. What strange array of chemicals are in the wine? How did they come to be? There are the ferments, the enzymes, the substrates, and the products. There in wine is found the great generalization: all life is fermentation. Nobody can discover the chemistry of wine without discovering, as did Louis Pasteur, the cause of much disease. How vivid is the claret, pressing its existence into the consciousness that watches it! If our small minds, for some convenience, divide this glass of wine, this universe, into parts—physics, biology, geology, astronomy, psychology, and so on—remember that nature does not know it! So let us put it all back together, not forgetting ultimately what it is for. Let it give us one more final pleasure: drink it and forget it all!
     

Friday, September 21, 2012

A Microsoft Life

A collection of annecdotes from his fifteen years with Microsoft: first in technical support, then in security response, and finally with xbox team. Humors do exist in the book. Do not expect glimpses into a coder's mind, but you will find a human being living his life.

Saturday, September 15, 2012

Why 2's Complement

Say we have 3 bits to represent integers. We have 8 possible numbers.
000
001
010
011
100
101
110
111
We wish to use one of these eight numbers to represent 0, half to represent positive and half to represent negatives. But with eight of them, we cannot do justice both to positives and to negatives! Well we wish to represent 2 and -2 in such a way that 2+(-2) becomes zero even in our new representation.
Let us use 000 to represent 0. Say now we use 001 to represent 1. Then what could be a reasonable representation of -1 so that 1+(-1) = 0. In other words,
  001
+ xxx
-----
  000
Obviously xxx = 110, just flip the bits in 001. That almost worked, but what then does 111 mean? It must be the flipped version of 000 meaning 111 = -0, which is not good - we don't want negative zero. However, if we add 1 to 111, it becomes 000, the extra one falls off the left end because we just have 3 bits in our system. Ok. Then say our algorithm for representing negative number is as follows:
flip the bits in the positive number and then add 1 to the result of flipping.
Let's see what we get.
000 ---flip---> 111 + 1 = 000 (great! there is no negative 0)
001 ---flip---> 110 + 1 = 111 (= -1)
010 ---flip---> 101 + 1 = 110 (= -2)
011 ---flip---> 100 + 1 = 101 (= -3)
100 ---flip---> 011 + 1 = 100 (= -4)
So, we have got what we wanted, almost! And notice that all negative numbers have their left most bit set to 1. So, it is quite easy to recognize them in this representation. Say if the computer sees 110, it immediately knows 110 is a negative number as its left most bit is 1. Now to find out the magnitude of 110, all the computer needs to do is to apply the same two steps again:
110 ---flip---> 001 + 1 = 010 = 2. That means 110 = -2!
In this way we got one number as 0, four to be negatives and three to be positives and we can do subtraction using adding unit.
This flip and then add 1 is just 2's complement.

Wednesday, September 12, 2012

Multiple password prompt issue with Tortoise SVN

What we need to do is
  1. To generate a public-key/private-key pair on the SVN server.
  2. Add the public key to the server's list of authorized keys.
  3. Save the private key into a file on my computer.
  4. Use PuTTy-gen to make this private-key PuTTy compatible.
  5. Add this PuTTy-compatible private key to Pageant.
From now on, Tortoise SVN will use the key from Pageant's list of keys. As the corresponding public key is in the server's list of authorized keys, the client-resident-tortoise's signature can be verified by the server and thus I do not need to enter password again and again.

The procedure is as follows:
  1. Download PuTTy, PuTTy-gen, and Pageant.
  2. Log in to SVN server.
  3. Type "ssh-keygen -b 1024 -t dsa -f mykey"
  4. You will be asked to enter a pass-phrase for the key-pair. Do not forget it. You need it while making it PuTTy-compatible on your machine.
  5. If you type "ls" you will see "mykey" and "mykey.pub" inside the current directory.
  6. "mykey" is the private key and "mykey.pub" is the public key.
  7. Type "mkdir .ssh" to create a folder that will hold the list of authorized keys on the server.
  8. Type "cp mykey.pub .ssh/authorized_keys" to add the public key into the list of authorized keys.
  9. Type "cat mykey" to get the contents of the private key.
  10. Copy the output of the "cat mykey" command and save it in a file with .ppk extension on your machine.
  11. Now startup PuTTy-gen and load this .ppk file you have just saved on your machine and hit generate.
  12. PuTTy-gen will generate a private key file compatible with it.
  13. Hit "Save private key" and save this generated private key file in another .ppk file.
  14. Now startup Pageant.
  15. Hit add key, and choose this compatible .ppk file. 
  16. As long as pageant is running, the Tortoise SVN gets the key from it instead of asking you for password again and again.
  17. ONE LITTLE PROBLEM: if you exit Pageant, then when you start it again, its list of keys will be empty. So, you need to add the compatible private key and to add it to Pageant it will require you to enter the pass-phrase you used while generating the private-key/public-key pair. But this is much better to enter the pass-phrase once per session of Pageant that to enter password many many times within a single session.

Tuesday, September 11, 2012

Superstition

According to Merriam-Webster:
"a belief or practice resulting from ignorance, fear of the unknown, trust in magic or chance, or a false conception of causation"
  • "What apparently grounds the widespread respect in which religions of all kinds are held is the sense that those who are religious are well intentioned, trying to lead morally good lives, earnest in their desire not to do evil, and to make amends for their transgressions."
William James in "The Varieties of Religious Experiences":
"There is religious fear, religious love, religious awe, religious joy, and so forth. But religious love is only man's natural emotion of love directed to a religious object; religious fear is only the ordinary fear of commerce, so to speak, the common quaking of the human breast, in so far as the notion of divine retribution may arouse it; religious awe is the same organic thrill which we feel in a forest at twilight, or in a mountain gorge; only this time it comes over us at the though of our supernatural relations."

Friday, September 7, 2012

Web Basics

  • Client (Web Browser) and server (Web Server) usually talks using HTTP over TCP. UDP-based RTP is better for streaming.
  • Web Browser can read HTML, XHTML and can show pages written in these languages.
  • Cascaded Style Sheets (CSS) help separate design (color, font, etc.) from contents in HTML, XHTML, or XML. CSS has three variants: inline, internal, and external. Inline overrides Internal and Internal overrides External, hence cascaded. The cascading allows controlling individual elements (inline) or page (internal) while having a global control (external).
  • XML is mainly used for data structuring and all tags are user-defined.
  • Extensible Stylesheet Language Transformations (XSLT) helps transform XML into Browser-friendly formats.
  • Client-side scripts or "embedded scripts" help make otherwise static pages, dynamic or interactive. They are kind-of like applets.
  • JavaScript and VBScript are the most popular client-side scripts. 
  • Cookie is kind of cache-info put into my machine by a site (through client-side scripts) to which I visited previously. It has a name, value, and expiry date.  
  • Emails are sent to SMTP server and received from POP3 server. 
  • Active Server Pages (ASP), PHP (originally Personal Home Page, nowadays PHP: Hypertext Preprocessor), and Practical Extraction and Reporting Language (PERL) are popular server-side scripting languages. 
  • Common Gateway Interface (CGI): A script or executable program is a CGI script if it is inside of and executable by the server, triggered by the browser, and the result can be displayed on the browser. So, CGI scripts or programs can be written in C/C++, PERL, PHP, and ASP.

Thursday, September 6, 2012

Web Design

Wireshark issue on Ubuntu

After installation of wireshark on ubuntu, it was not showing any network interface to start capturing packets. Did the following:
sudo dpkg-reconfigure wireshark-common
sudo usermod -a -G wireshark $USER
sudo reboot

Tuesday, September 4, 2012

Octave on Ubuntu

  • To run Octave script without getting into Octave environment, type: octave --silent --eval 'myfactorial(5)' where --silent gets rid of some annoying prints regarding warranties, etc.
  • To turn on syntax highlighting for Octave in vi editor: download octave.vim from here, and copy it into /usr/share/vim/vim73/syntax. Inside /usr/share/vim/vim73, you will find filetype.vim, open it and replace all occurrences of the word matlab with octave and save it. 
  • A less geeky editor would be QtOctave.
  • A beautiful site

Saturday, September 1, 2012

Fractions, from decimal to binary

Say we have 0.625 and we want to convert it into binary. We can write 0.625 = 1*0.5+0*0.25+1*0.125, so in binary 0.625 becomes .101. But we can do this conversion from decimal fraction to binary fraction in a more systematic way:
0.625 * 2 = 1.25 (1.25 >= 1) so first digit after radix point is 1, remainder 1.25-1.0 = 0.25. Now 0.25 * 2 = 0.5 < 1, so 2nd digit is 0; now 0.5*2 = 1.0 >= 1, so 3rd digit after radix point is again 1 and as now remainder = 1-1 = 0, we are finished. Though the second method is more systematic, it is not very clear how it is equivalent to the first one. In the first one we comare 0.625 with 0.5 and as 0.625 > 0.5, there will be a 1 for 0.5's place after radix point and so on. Now, checking whether 0.625 is greater than or equal 0.5 is equivalent to checking if 2*0.625 is greater than or equal to 2*0.5 = 1.0; next, the remainder is 0.625-0.5 = 0.125, now comparing 0.125 and 0.25 is equivalent to comparing 2*(2*0.625-2*0.5) and 2*(2*0.25) or 2*0.25 and 1.0; and so on...

Halving issue in Binary Search

Here is the way to go. In Java: 
mid = (high+low)/2 may give incorrect result. The maximum positive value (in Java) of int is, 2^(31)-1 or 2147483647 (in binary     0111 1111 1111 1111 1111 1111 1111 1111). The problem is that the intermediate value (high+low) could exceed this maximum int value. For example, if high and low, both are equal to 2^(31)-1, then the intermediate value becomes 

 high        : 0111 1111 1111 1111 1111 1111 1111 1111

+low         : 0111 1111 1111 1111 1111 1111 1111 1111
------------------------------------------------------
Intermediate : 1111 1111 1111 1111 1111 1111 1111 1110

And in 2's complement, this intermediate is


2's complement = 1's complement(Intermediate)+1, hence


(1's complement) 0000 0000 0000 0000 0000 0000 0000 0001

(            +1)                                       1
--------------------------------------------------------
                 0000 0000 0000 0000 0000 0000 0000 0010

So, intermediate = -2. This -2 is like

[1111 1111 1111 1111 1111 1111 1111 11]10, where [...] is the sign extension.
Thus mid = -2/2 = -1, which is incorrect.

If we use mid = low+(high-low)/2, that overflow (intermediate exceeding maximum allowed value of
int) never happens, thus gives correct result.
Another incorrect way is mid = (high+low) >> 1; because the overflow has happened already before the shift and ">>" is the signed shift in Java, thus the intermediate 
[1111 1111 1111 1111 1111 1111 1111 11]10 becomes 
[1111 1111 1111 1111 1111 1111 1111 111]1, which is -1. 
The best way is, mid = (high+low) >>> 1. Because ">>>" is the unsigned shift in Java, the intermediate 
[1111 1111 1111 1111 1111 1111 1111 11]10 becomes
[0111 1111 1111 1111 1111 1111 1111 11]11 which is actually the correct mid (= 2^(31)-1).