Sunday, May 10, 2015

Signed vs. Unsigned Integers in C

2's complement Encoding

The most common binary encoding technique for integers is called 2's complement. A signed integer is represented with “N” bits will have a range from -2N-1 to 2N-1 – 1.

The encoding algorithm is as follows.

1. The high-order (leftmost) bit of the word is the sign bit. If the sign bit is 1, the number is negative; otherwise, the number is positive.

2. Positive numbers follow the normal binary sequence.

    0 = 000...0002
    1 = 000...0012
          ...

3. In an N-bit word, omitting the sign bit, there are “N-1” bits for the positive integers which can represent the integers from 0 to 2N-1 – 1.

4. To negate an integer, complement all the bits in the word and then add 1 to the result. For example, to form negative integer -9, start with 9 = 000..10012, complement all the bits to get   111..01102, then add 1 to get -9 = 111..01112 = 247.

5. The maximum negative value, -2N-1, does not have a positive equivalent; negating this value produces the same value.

1's complement Encoding

Another encoding scheme which is used for representing integers is 1's complement. In this scheme, each positive number is represented by normal binary encoding. To obtain a negative number, each bit is simply complemented. For example, to form negative integer -9, start with 9 = 000..10012 and complement each bit to get -9 = 111..01102.

The range of numbers that can be represented for a N-bit word is -(2N-1 – 1) to 2N-1 – 1. 1's complement represents 1 less negative value and has both “-0” and “0”.

Sign Magnitude Encoding

The sign magnitude encoding is similar to the 1's complement encoding. To negate an integer, it just complements the sign bit. For example, to form negative integer -9, start with 9 = 000..10012 and complement the sign bit to get -9 = 100..10012.

The range of numbers that can be represented for a N-bit word is -(2N-1 – 1) to 2N-1 – 1. 1's complement represents 1 less negative value and has both “-0” and “0”.

Signed vs. Unsigned

Unsigned integers are always stored using straight binary notation regardless of whether the signed types use 2-s complement, 1-s complement or sign magnitude notation. For example, in 2-s complement notation, the bit pattern 111...111 (n bits long) can represent either “-1” (using the signed notation) or 2N – 1 (using the unsigned notation). The integers from “0” to 2N-1 – 1 are represented identically in both signed and unsigned notations.

Whether an integer is signed or unsigned affects the operations performed on it. All arithmetic operations on unsigned integers behave according to the rules of modular (congruence) arithmetic modulo 2N. For example, adding 1 to the largest value of an unsigned type is guaranteed to produce “0”. The behaviour of overflow is well-defined.

Expressions that mix signed and unsigned integers are often forced to use unsigned operations. When a negative signed integer is promoted to an unsigned integer of the same or larger type, it is first promoted to the signed equivalent of the larger type, then converted to the unsigned value.

The conversion rules are as follows.



Mahakavi Kumaran Asan -- A Brief Tribute

Malayalam literature has a very prominent place in Indian literature scene. The more serious taste developed in the 16th century but the actual, unrecorded contributions date back to Sangam Age. Many noted and versatile novelists, poets, dramatists, philosophers, astronomoers, linguists have combined over the centuries to nourish and flourish Malayalam literature. Among all these writers, Mahakavi Kumaran Asan was undoubtedly the biggest poetical influence.

In a short career of just 16 years, Kumaran "Kumaru" Asan, left an indelible mark on the Malayalam poetry. Romance and love were the most widespread theme of his poems. The publication of Veena Poovu (The Fallen Flower), a long elegiac poem, in 1908 initiated the tradition of love in Malayalam poetry. The major poems of Asan, which won immediate and unqualified acclaim from all those who kept their ears open for a new voice, are Nalini (1911), Leela (1914), Duravastha (1923), Chandalabhikshuki (1923) Karuna (1925) all of which are story-poems, Prarodanam (1919) an elegy on the death of Raja Raja Varma, and Chintavishtayaya Seeta, a dramatic monologue. His Gramavrikashathile Kuyil, Buddhacharitam, Balaramayanam and short lyrics are also poems of considerable merit.

He was born on 12 April 1853 in Ezhava business community at Kayikkara village, Chirayinkeezhu taluk, north of Thiruvananthapuram, Kerala. Belonging to a backward caste by birth, his major active life was involved in agitation against the Brahmanical rules which deprived the lower castes of their liberties as well as their own primitive custom and ceremonies. He worked as a member and general secretary of Sree Narayana Dharma Paripalana Sangham and was a highly respected member of the Tiruvitamkur Legislature.

Sanskrit was Asan's favourite language. He left his primary teacher job to study Sanskrit in Bangalore and Calcutta. He was equally proficient in English. The exposure to social and spiritual resurgence in Bengal instilled a sense of self-belief in him that change can be brought around in caste-ridden Kerala of those times. His heroes and heroines refused to be swayed by parental authority or social injunctions and chartered their own path of love and discover their human worth above the society defined caste and prejudices.

Along with Vallathol Narayana Menon and Ulloor.S.Parameswara Iyer, Asan weaved a new and vibrant trajectory for the Malayalam poetry. Professor G. Kumar Pillai quips the following summary of their achievement.
What Kumaran Asan and his contemporaries did was to revive the healthy element in our tradition and to extend it under the impact of western thought and literature. They did not reject the past, nor were they blind imitators of the west. Their greatest achievement was to recapture the spirit of Cherussery and Ezhuthassan and to forge another synthesis in a new context. They discarded the decadent neoclassicism in which they themselves were matured and restored the central tradition of our poetry to which they added a new dimension. The central tradition had two elements in it _ the Cherusseri style _ simple, sweet and beautiful, and the Ezhuthassan style _ deep, intense and grand. Of the two dominant figures in the first generation of our renaissance poetry, Vallathol, was the follower of the Cherusseri style while Asan inherited the Ezhuthassan style.
On 13 June, 1965, Kumaran Asan Memorial Association was established in Chennai with an aim to promote Malayalam literature. The association instituted the Asan Smaraka Kavitha Puraskaarama in 1985 to honour outstanding works of Tamil and Malayalam poetry. Now, it is conferred only on Malayalam poets. The award carries a prize of Rs. 30,000. A World Poetry Prize was also constituted. The award for 2015 has been given to Arab poet Adonis.

He regarded a life of transient brilliance and devotion to be the one to be picked over the life of long servitude to social and cultural absurdities. And, his work is full of this message. What life shall we choose?