How to determine whether the binary string is a palindrome?

Method-1: Using the C++ standard library std::bitset.

 1 #include <iostream>
 2 #include <bitset>
 3 #include <string>
 4 
 5 using namespace std;
 6 
 7 int main(int argc, char* argv[])
 8 {
 9   int n, nbits;
10 
11   cout << "Enter a number: ";
12   cin >> n;
13 
14   bitset<sizeof(short) * 8> b(n);
15   cout << "Binary equivalent of " << n << ": " << b.to_string() << endl;
16 
17   string bits = b.to_string();
18   string rbits = bits;
19   reverse(rbits.begin(), rbits.end());
20   
21   if (bits == rbits)
22     cout << bits << " == " << rbits << endl;
23   else
24     cout << bits << " != " << rbits << endl;
25 
26   return 0;
27 }

The above method is inefficient both in space and running time. For the sake of brevity, the extra copy of  ‘rbits‘  is used. The above method runs approximately in linear time (‘n‘ times for converting the bits to string and another ‘n‘ for reversing the string).

Powered by ScribeFire.

How to build and install Boost.Log?

Update: Boost.Log is now part of the entire set of Boost Libraries since v1.54. The instructions on how to build Boost Log is available here: Configuring and building the Boost Log library

The following instructions are valid only for v1.0 of Boost Log.

This is a brain dump of the method followed by me to install Boost.Log. One important note before you build and install boost libraries: Make sure you have installed a threading library like pthreads. Most package managers should have them.

Now lets get on to the build steps:

  1. If you have installed Boost already from source, then its fine continue to next step. Otherwise download it from here (this link is for latest version as of this writing: v1.45). Extract the boost libraries at say: /opt. We can build the libraries along with Boost.Log.
  2. Download Boost.Log from Sourceforge.
  3. Extract the Boost.Log source archive in a folder – say /opt.
  4. Copy the log folder in: /opt/boost-log-1.0/boost to your boost source directory /opt/Boost_1_45_0/boost (assuming that you have extracted it in /opt).
  5. Copy the log folder in: /opt/boost-log-1.0/libs to your boost libs directory /opt/Boost_1_45_0/libs (assuming that you have extracted it in /opt).
  6. If you haven’t installed other Boost libraries, then follow these steps:
  • cd /opt/Boost_1_45_0
  • .bootstrap.sh --show-libraries — this will list all the libraries that will be built and installed. You should see log listed as part of it.
  • .bootstrap.sh --with-libraries=all --prefix=/usr/local --includedir=/usr/local/include --libdir=/usr/local/lib
  • ./bjam install

Finally make sure $LD_LIBRARY_PATH has /usr/local/lib (the path specified in bjam to install the built libraries) as part of it. If it is not edit your ~/.bashrc and add the following:

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
export LD_LIBRARY_PATH

How to get the svn revision number of a particular commit object in git?

If you know the ‘SHA1 hash key’ for a commit you made to git and if you want to know the corresponding svn revision number here is what you do:

$ git svn log --show-commit | grep GIT-SHA1-KEY

GIT-SHA1-KEY‘ is the 128-bit key that git assigns for each commit.

If you know of any other ways to fetch the svn revision number of a git commit object, please post it in the comments. Thanks!

Lectures on advanced topics in programming – 1

Google videos has interesting videos about lectures that were given in Google. The lecture series was aptly named: Advance Topics in Programming Languages:

Concurrency and Message Passing in Newsqueak – the lecture is given by the author of the famous book “The Unix programming Environment” – Rob Pike. He talks about the concurrent programming language ‘Newsqueak’ that he designed.


Parametric Polymorphism – this lecture is all about types, polymorphism, isomorphism, etc…

Transactional Memory at Sun – this lecture talks about what transactional memory is? why is it necessary? what are the current trends? The talk is mostly about the research thats being done at Sun Microsystems on Transactional Memory.

Faith, Evolution, and Programming Languages – I tried to give a gist about the lecture but I could not come up with a crisp and clear write up. So I am quoting it directly as given in the abstract:

Faith and evolution provide complementary – and sometimes conflicting–models of the world, and they also can model the adoption of programming languages. Adherents of competing paradigms, such as functional and object-oriented programming, often appear motivated by faith. Families of related languages, such as C, C++, Java, and C#, may arise from pressures of evolution. As designers of languages, adoption rates provide us with scientific data, but the belief that elegant designs are better is a matter of faith. This talk traces one concept, second-order quantification, from its inception in the symbolic logic of Frege through to the generic features introduced in Java 5, touching on features of faith and evolution. The remarkable correspondence between natural deduction and functional programming informed the design of type classes in Haskell. Generics in Java evolved directly from Haskell type classes, and are designed to support evolution from legacy code to generic code. Links, a successor to Haskell aimed at AJAX-style three-tier web applications, aims to reconcile some of the conflict between dynamic and static approaches to
typing.

End User Software Engineering – as the topic says the lecture is about software engineering at the end-user level.

Resource Aware Programming – this lecture is on embedded systems related. The title says it all. The talk is about the resource-bounded computations.

Introduction To Digital Identity – this talk is all about identity and how to manage them. Its a nice introductory talk.

Hardware/Software Hacking: Joining the Real and the Virtual – this talk tells about some techniques of hardware and software hacking.