Sunday, September 30, 2007

Subtraction in Binary

What do you use to subtract two binary numbers?
two's compliment?
Why to have so complicated approach ( of course on Paper )
Just use our basic decimal system approach and subtract the number normally.
For e.g.,
binary decimal
100 4
011 3

subtract 4-3 = 1
Just think that 4 is hundred in binary and 3 is eleven ( as it looks like visually ).
Now subtract 11 from 100 , you will get 89. Now replace 8 by 0 and 9 by 1
you will get 1 as the answer in binary.

When you subtract two binary numbers as decimal numbers, you have four possible numbers 0,1 , 8 and 9. Now since the answer should be in binary, 8 is even so replace by 0 ( as 8 mod 2 = 0) and 9 is odd so replace by 1 ( as 9 mod 2 = 1 ).

See how easy it goes !

Thursday, September 27, 2007

Singly linked list as Doubly

With the linked list the major concern is the space consumed by the pointer in the structure.
For singly linked list, there is one extra pointer in each node to point to the next node.
Similarly in doubly linked list, there are two extra pointers in each node, one point to previous node and other point to next node.
Now depending upon the machine word size, if it is 32 bit, the pointer consume 4 bytes and in 64 bit machine it consumes 64 bits. Which is quite a bit.
I have written a code in C++, which just take the space of a singly linked list but it can be used to traverse both ways like doubly linked list.

#include
using std::cout;
using std::endl;


template
class NODE {
public:
NODE() {
next = NULL;
}
NODE(const TYPE data) : data(data), next(NULL) { }
TYPE getData() {
return data;
}
NODE *getNext() {
return next;
}
void setNext(long next) {
this->next = reinterpret_cast(next);
}
private:
TYPE data;
NODE *next;
};

template
class LIST {
public:
LIST() {
XORdata = head = tail = NULL;
}
void add(TYPE data);
void traverseForward();
void traverseBackward();
private:
NODE *head;
NODE *tail;
NODE *XORdata;
};

template
void LIST::traverseBackward() {
NODE *tempTail = tail;
NODE *tempTail1 = tail;
NODE *tempXOR = XORdata;
while(tempTail) {
cout << "Data : " <<>getData() << p1 =" reinterpret_cast<"> (tempTail->getNext());
long p2 = reinterpret_cast<> (tempXOR);
tempTail = reinterpret_cast *>(p1^p2);
tempXOR = tempTail1;
tempTail1 = tempTail;
}
}

template
void LIST::traverseForward() {
NODE *tempHead = head;
NODE *tempHead1 = head;
NODE *tempXOR = NULL;
while(tempHead) {
cout << "Data : " <<>getData() <<>getNext()) break;
long p1 = reinterpret_cast<> (tempHead->getNext());
long p2 = reinterpret_cast<> (tempXOR);
tempHead = reinterpret_cast *>(p1^p2);
tempXOR = tempHead1;
tempHead1 = tempHead;
}
}

template
void LIST::add(TYPE data) {
if(NULL == head) {
tail = head = new NODE(data);
} else {
NODE *temp = new NODE(data);
long p1 = reinterpret_cast(XORdata);
long p2 = reinterpret_cast(temp);
tail->setNext(p1^p2);
XORdata = tail;
tail = temp;
}
}

int main(int argc, char **argv, char **arge) try {
LIST list;
for(int i = 10; i < 16; ++i )
list.add ( i);
list.traverseForward();
cout << "Now moving backward !!!" << endl;
list.traverseBackward();
return 0;
} catch(...) {
cout << "Uncaught exception!" << endl;
}

Tuesday, September 25, 2007

Cricket Puzzle

Contrary to an Indian, I don't like cricket at all ( at least before twenty 20 ).
Though when I came through the below mentioned cricket puzzle and having interest in solving puzzles. I somehow gained interest in knowing the rules. So people who know cricket should have a look.
Here it goes:
There are last 2 balls to bowl. Both the batsman are on 94 each.
The team require 7 runs to win. Both has to score century as well.
How will they help their team to win.

Thursday, September 20, 2007

Atomic Compare and Swap algorithm

Is there anyone who knows how to make CSA in C/C++ to make it atomic ?
This would help in having producer-consumer lock free.

Why VTABLE is required for Abstract class in C++

I am not sure why VTABLE is required in C++ after all it is just an interface and it always refer to its derived classes only. One can manipulate the pointer to upcast but that has got no meaning ( logical ).
Therefore, my main question is why not optimize it and remove the VTABLE from abstract class.
I am looking for a satisfying answer.