Skip to main content

Posts

ASCII Magic for Upper and Lower case

We've 26 alphabets in English and using ASCII code we're representing it in the computer system. As an example, 'A' is represented as 65 in decimal, and 'a' is represented as '97' in decimal. Now check the binary counterpart of 65 and 97. 65 = 01000001 97 = 01100001 From the above binary representation, it's quite clear that the upper case letter differs from the lower case letter in binary representation, exactly in a one-bit position. The above example shows these two codes differ in the 5th bit. This is true for all 26 English alphabets and can easily be deduced to an implementation in C++ to convert any lower-case English letter to an upper-case letter. The following code demonstrates it: #define toUpper(ch) ((ch >= 'a' && ch <='z') ? ch & 0x5f : ch) int _tmain(int argc, _TCHAR* argv[]) { printf("Upper case conversion: %c\n", toUpper('b')); return 0; } *Inspired by Great Peo

Exceptions of floating point normalization

Floating point normalization has a great usage for computing anything very near to accuracy. A floating point number is consists of: Mantissa or significand. Exponent. Say, I've a number 123.75. Its a floating point number. It has integer significand, 12375 and exponent -2. So arithmatic representation is 12375 x 10 -2 . How to normalize a floating point number? - By shifting the mantissa to left until a 1 appears in most significant bits(HO). Hence, the normalized representation will be 1.2375 x 10 +2 . Most of the time for normalized number this bit is hidden as it happens to be 1. This is hidden bit. Now the question when we can't normalize a floating point number? - There are two such situations: We can't normalize zero(0). The floating point representation of Zero doesn't contain any 1 bit. However, IEEE representation for +0 and -0 has different significance. We also can't normalize a floating point number whose most significant bits in mant

MBCS/Unicode enabled C++ string class.

TCHAR, the generic text mapping data type. This is Microsoft specific extension and is not ANSI-compatible. I have used this extension to create my small prototype MBCS/Unicode compatible class. This class consists of all the basic functionality required to represent a minimal string class. I have given the name for this class as "CStringUNI". // Header File #pragma once const int INIT_ALLOC_SIZE = 10; class CStringUNI { private: TCHAR *m_szBuffer; TCHAR *AllocateMemory(size_t size); public: CStringUNI(); CStringUNI(const CStringUNI&); CStringUNI(const TCHAR*); CStringUNI& operator=(const TCHAR*); CStringUNI& operator=(const CStringUNI&); CStringUNI& operator+=(const CStringUNI&); // Access Operator TCHAR operator[](const size_t n)const; TCHAR *GetBuffer() const; bool operator==(const TCHAR*) const; bool operator==(const CStringUNI&) const; virtual ~CStringUNI(); friend CStringUNI operator+(const CStringUNI& lhs, const

Importance of 'const' in C++

Lets declare a class 'Demo' class Demo { private: char a; void TestConst(char* const pData) const { // pData = "test"; // char* const protects // pData++; // pointer! // a = 'a'; // const for function // protects data member. } }; So, when we are working with C++, all data members irrespective of access specification, can be protected with 'const', appended to the declaration/definition.

Use of Memoization in Recursion

Memoization can be used to reduce function calls or computations. In the following example of Fibonacci series generation through C++, I will use normal recursion and then will change the same for memoization( http://en.wikipedia.org/wiki/Memoization ) to show the reduction of the function call. This is a simple demo implementation. static int counter = 0; int normalfibrecursion(int n) { if( n < 1) { return 0; } if(n == 1) { return 1; } if(n < 3) { return 1; } else { cout << "Call normalfibrecursion(" << n-2 << ") and normalfibrecursion(" << n-1 << ")\n"; counter++; return normalfibrecursion(n-2) + normalfibrecursion(n-1); } } In the above-mentioned simple prototype implementation for the Fibonacci series of n=10, the total function call comes to (54*2) times. This is usually very high and it will grow exponentially as n increases. Below mentioned picture demonstrates the fact: USE OF MEM

Generic Swap without using template

The idea behind this was, I wanted to write a function that will take two parameters of the same type as a parameter and then it will swap them. It is a kind of generic swap but without the use of a C++ template. So the best way of doing it using "void *" as a parameter. As we know "void *" represents any arbitrary type that actually eases my job of writing a generic swap function. So, the function signature can be like below: void swap(void *arg1, void *arg2); "void *" points to the starting address of the arbitrary location in the memory, irrespective of the bit pattern. Try to write the function like the below: void swap(void *arg1, void *arg2) { void temp = *arg1; arg1 = *arg2; *arg = temp; } Oops, this is full of errors. 1. We can't declare a variable of type "void". 2. "void *" can't be dereferenced. 3. We also interested in swapping values. So, number of bytes making up the values to be pass