Archive

Archive for the ‘Academics’ Category

Check if a given number is power of 2

March 8th, 2011 No comments

Given a number N, we will have to check if N is a power of 2. Powers of 2 include 1,2,4,8,16,32… and so on. Following are FIVE different approaches to solve the problem.

I. This is the shortest method. We perform bitwise AND operation on the given number N and N-1. If the result is 0, then N is a power of 2. If the result is non-zero, it’s not a power of two.
Bitwise AND Operator


int power_of_two(int n)
{
return !(n&(n-1));
}

II. This method uses the basic definition of a logarithmic function. For N to be a power of 2,(log N to the base 2) must be a whole number and not a fraction.


int power_of_two(int n)
{
float x;
x=log(n)/log(2);
if(x-(int)x==0.0) return 1;
else return 0;
}

III. This method makes use of the fact that any number that is a power of 2, has exactly ONE 1s in its binary representation. Hence the above code just counts the no. of ones in the binary representation of the given number.


int power_of_two(int n)
{
int count=0,rem;
while(n>0)
{
rem=n%2;
if(rem==1||n==1) count++;
if(count>=2) break;
n=n/2;
}
if(count==1) return 1;
else return 0;
}

IV. This is a brute-force method which generates powers of two and checks if it is equal to the given number N.


int power_of_two(int n)
{
int count=0,temp;
do
{
temp=(int)pow(2,count);
if(temp==n) return 1;
count++;
}while( temp < n );
return 0;
}

V. This is a recursive implementation. When a number N which is a power of two is divided by 2, the quotient obtained will also be a power of two.


int power_of_two(int n)
{
if(n==1) return 1;
if(n%2!=0) return 0;
if(!(power_of_two(n/2))) return 0;
}

Run MASM 8086 Assembler in Ubuntu or Windows 7(x64) using DOSBox

March 4th, 2011 41 comments

Here’s how to run 16 bit DOS executables like the MASM assembler or Turbo C compiler in Ubuntu (GNU/Linux) or in 64 bit editions of Windows 7 using DOSBox, a DOS environment emulator. DOSBox is available for Linux as well as Windows.

DOSBox installation

For Ubuntu users (using repository)
Open the terminal and type in the following commands to download and install DOSbox in Ubuntu
sudo apt-get update
sudo apt-get install dosbox

You will find it installed under Applications_Menu->Games->DOSBox Emulator

For other GNU/Linux users
Download DOSbox from here.
Open terminal and cd to the directory containing the downloaded tar.gz file. Type in the following commands to build and install :
tar -xzvf dosbox-0.74.tar.gz
cd dosbox-0.74
./configure
make

Check the src subdir for the binary.

For Windows users
Download DOSbox from here.
Run the downloaded .exe file and install it like any other software.

Now that you’ve installed DOSBox, you’ll be able to run any 16bit or 32bit DOS executable inside it.

Download the 8086 MASM Assembler from here. The zip file contains the following files :
masm.exe, tasm.exe, link.exe, bin2hex.exe, exe2bin.exe, td.exe, edit.com and debug.exe

Windows users extract the .zip file into C:\ so that the path C:\8086 contains all the above mentioned files. GNU/Linux users can extract it and place it in say /home/prashanth/8086

Launch DOSBox and type the following commands :
For Linux users :
mount c /home/prashanth/8086
c:

For Windows :
mount c c:\8086
c:

DOSBox in Ubuntu

DOSBox running in Ubuntu

Now the contents of the folder /home/prashanth/8086 or c:\8086 is mounted as c: drive inside the DOS emulator. You can assemble programs inside DOSBox as you do in your Microprocessor Lab under Windows XP; i.e your usual sequence of commands -
edit file.asm
masm file.asm
link file
debug file.exe

When you are done, type exit to quit DOSBox.

P.S : For GNU/Linux users, there’s an alternative assembler known as the NASM. NASM is considered to be one of the most popular assemblers for GNU/Linux.

Downloads :
DOSBox for Linux : dosbox-0.74.tar.gz
DOSBox for Windows : DOSBox0.74-win32-installer.exe
8086 Assembler : 8086_Assembler.zip

CET : How to choose the right Engineering College for your rank ?

May 23rd, 2010 151 comments

As soon as you get your CET rank, most of the you are in total dilemma and confusion over which college and branch to choose for your rank. If you are one of them, this post is just for YOU. The question arises – how to decide to choose a college when there are hundreds of engineering colleges in Karnataka.

• Will you get admission in good college or not?
• What will be the Cut off percentage of different colleges ?
• What are the different courses available in different colleges ?
• Which branch should I take ?
• Which college I will get for my rank ?

First things first, shortlist the colleges and the places that you like (keeping your rank in mind : having rank over 10k and expecting to get E&C in RVCE, that’s height of craziness). This is where Read more…

Autonomous Engineering Colleges In Karnataka

July 1st, 2009 36 comments

Here is a list of autonomous engineering colleges in Karnataka that have intake under CET. First things first, what’s an autonomous college or what is autonomy ?

Need for Autonomy :
The affiliating system of colleges was originally designed when their number in a university was small. The university could then effectively oversee the working of the colleges, act as an examining body and award degrees on their behalf. The system has now become unwieldy and it is becoming increasingly difficult for a university to attend to the varied needs of individual colleges. The colleges do not have the freedom to modernise their curricula or make them locally relevant. The regulations of the university and its common system, governing all colleges alike, irrespective of their characteristic strengths, weaknesses and locations, have affected the academic development of individual colleges. Colleges that have the potential for offering programmes of a higher standard do not have the freedom to offer them. Read more…

Engineering CutOff Ranks Of Colleges in Karnataka

May 27th, 2009 376 comments

Students after getting CET results are not sure that in which college they may get a seat and in which branch. So I have put up the cutoff ranks of various top engineering colleges in Karnataka. The data is taken from 2007 CET counseling and there might be very slight difference in cutoff this year. However, the trend will remain the same.

I don’t have reservation under any category and hence cannot get Computer Science branch in any top engineering college. May be I should take Mechanics. Or if luck is on my side, nobody should take Computer as the IT industry has collapsed and there are thousands of job cuts. Hope it works that way ;)
The following cutoff ranks are with respect to General Merit (GM) seats only. Read more…