Saturday, December 21, 2013

What happens when an integer value goes out of range?

Signed integer variables:

Range of short int : -32,768 to 32,767
Range of long int : -2147483648 to 2147483647


Unsigned integer variables:

Range of short int : 0 to 65535
Range of long int : 0 to 4294967295

When we initialize a integer variable or assign any value to it , if the value is in range of our defined integer then the program works normally as it should.
But if the value goes out of range then there we know that the value that resides in the variable is a garbage or any random value. Is that value actually a random one?
The answer is no , that is a calculated value within the range of the integer which is calculated by the compiler.
Now, how the value is calculated of it goes outside the range and brought back in the range.

The algorithm used is:

y = x % range;
if y in range 
the value is y
else
y = y % range;


The modulus operator(%)  brings back the value gone out of range because the remainder to this expression is always in the range.

The range is taken to be the (negative end range + positive end range + 1). For unsigned variables there is no negative range so , (positive end range + 1). We have to compute the remainder by taking the nearest possible multiple of range to the value x that we assign to the integer. We can also compute the remainder by taking negative multiples if required.

Here is a video to demonstrate the process..



Sunday, December 1, 2013

Danger's associated with a protected data member in C++

We usually specify protected to data members to use them in derived class in Inheritance, but there is a problem with protected data member's that they can be used by any other class just by inheriting the class which contains that data member.
Lets watch a video to understand the problem with protected and how to fix it.


                                                                     

Wednesday, November 6, 2013

How to miantain your laptop's battery health?

Most of us usually destroy our batteries in a year or so. I destroyed mine in a year and a half to 50%.
Now i have purchased a new  laptop , so i would like to take care of my laptops battery and make it last longer.

After surfing on the web I found 2-3 articles really helpful. i m gonna share with you the brief measures that u can take to extend your battery health.


  • First of all , the most important point that has to be  taken into consideration is that batteries don't want to get charged upto 100%!! ,  rather the want a user that could drain it out to 40-30 % and charge it to 80%. charging your battery to 100% and not letting it go down below 60 or 50 % is not actually helping the battery. Batteries want to get charged between 40 to 80 % usually. This can double the charge and discharge cycles in some cases!!

  • also heat generated by your laptop can affect  your batteries health , so please be careful about your laptop's temperature, avoid using it on lap, use a table or desk.

  • drain out your battery completely once in month to keep it healthy


so these a some important precautions you can take to maintain to  batteries upto 3 or maybe 4 yrs.
Good Luck!!

Sunday, February 3, 2013

how to mention a user in sudoers list

Most the time when we add a new user to group and try to run sudo command on that user,we encounter error :

             this user is not present in the sudoers list

to solve this problems we have to easy methods.
one can be used while creating a new user,and another is for those who have already created the user and are wondering to use sudo with that user.


First we will go with the those who have already created the user :

  1. login with a use who is already present in the sudoers list
  2. open the teminal window and type in sudo adduser username sudo.
and you are set.

now lets come to those who want to create a new user and mention it in the sudoers list:

  1. you have to add option while creating a new user.
  2. just add the option -G sudo in the useradd command.
  3. for eg. useradd -u 500 -g dba -G sudo -d /home/oracle oracle

basically sudo is a group and all the users present in this group will have the full control over the sudo command.we are just adding the user to the sudo group.

resolving the error **Unexcpected Operator** in shell scripts

In Some Linux systems the full functionality of the sh or bash shell is not available.So to solve this we have to follow a few steps:

  1. open the .sh extension script file you are working with.
  2. find the clause #!/bin/sh. in most of the scripts its the very first line,
  3. just replace the #!/bin/sh with #!bin/bash.
  4. save the file and open the terminal window
  5. navigate to the directory where the file is present .
  6. now execute the command  bash ./filename.sh
and that's it , you are done with the error.