May 30, 2017

Assigning Binary Value to a Variable in C and C++

If you are programming in C and C++ then you may already know that C and C++ do not have native support for binary literal. So you can not simply write the following statement to give a variable a binary value:

int signal = 1110;

However, C and C++'s bitwise operators such as Left Shift operator << and Or operator | can be easily utilised to achieve binary value assignment like shown below:

int signal = 1 << 3 | 1 << 2 | 1 << 1;

The above statement assigns binary value 1110 to the variable signal. The way it works is the very first expression 1 << 3 creates a binary value 1000, and then the expression 1 << 2 creates a binary value 100 and finally the expression 1 << 1 creates a binary value 10. When all these three binary values are combined using the Or operators we get the binary value 1110 like shown below:

Expression            Binary equivalent
1 << 3            =     1000
1 << 2            =     0100
1 << 1            =     0010
                              1110 (after or operations of three expressions)

One advantage about this method of binary assignment is that there is no need for external libraries and such. The example program demonstrating the above method can be downloaded from the following link:


There is also a convenience functions library called imamB which I wrote recently which can be used for getting binary values of variables as a string and vice versa. To use the functions from imamB all you need to do is to include header and source files from imamB package to your project directory. imamB can be download from the following link:


March 19, 2017

Windows Install and use Python without admin rights

Sometimes it is useful to install Python in a preferred directory other than to a default Windows partition directory. The main reasons for doing this are given below:
  1. Have multiple Python versions and run them as per requirements (use Python versions 2.7 and 3 simultaneously on the same system)
  2. Have Python Interpreter on a portable disk (Python on the go)
  3. Tryout experimental and latest Python versions and Python packages/libraries (leave the default system Python Interpreter unaltered)
Installing Python without admin rights:

Step 1: Download Python installer(msi file) from official website

Step 2: Open up Windows Command Prompt where msi file is downloaded and execute the following command,

msiexec /a python-2.7.10.msi /qb TARGETDIR=G:\Python27

Here, TARGETDIR specifies the target folder where Python will be installed.

Once setup is finished you should be able to find python.exe (Python Interpreter) and IDLE (Python Integrated Development Environment) in the G:\python27 and G:\Python27\Lib\idlelib folders respectively.

Step 3: Install pip Python utility script for installing Python packages/libraries by downloading file and executing the following command from the G:\Python27 folder,


Step 4: Install Python packages/libraries using pip by executing the following commands from G:\Python,

python.exe -m pip install numpy

Here, numpy is Python package/library name which will be download and installed to Python Interpreter directory by the pip utility. If a package/library is not available in the standard Python pip repository then you can download pip install-able (whl files) Python packages/libraries from pythonlibs and install them by executing following command from G:\Python27, assuming whl files are placed in the same folder,

python.exe -m pip install numpy‑1.11.3+mkl‑cp27‑cp27m‑win32.whl


February 22, 2017

Linux .desktop file run a executable file from the same directory

Desktop files in GNU/Linux define shortcuts for conveniently launching installed applications in the system from system menus and file managers. They can be found in many different places such as in /usr/share/applications directory for system wide applications and in ~/.local/share/applications for user applications. These files can be opened in any text editor program to edit application executable file names or for adding new options to application executables. A typical desktop file may look like below:

[Desktop Entry]

GenericName=Package Installer
Comment=Copy package files to your system

Each key and value pair defines different attributes of the application launcher. For example the Name key assigns a name for the application launcher which would appear in the system menus and file managers. The most important key is Exec which tells the name of the application executable file. The value of the Exec key can be either just the application executable file name or absolute address of the application executable file like shown below:


However, sometimes it is preferable to tell Exec key that the application executable file is located in the same directory as the desktop file. This can be easily achieved with the %k code which is available to the Exec key. The %k code in the Exec key value gives us the absolute location of the desktop file as either a URI (if for example gotten from the vfolder system) or a local filename or empty if no location is known. Now, to run a application executable in the same directory as desktop file, Exec key can have the following value:

Exec=xdg-open "%k"/

The above Exec key value also ensures portability across different GNU/Linux distributions and can be used for many different application scenarios such as shipping portable application packages.

That's all for today