Give a fast way to multiply a number by 7
Posted on January 3, 2010
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
Multiply by 8 (left shift by 3 bits) and then subtract the number.
Bresenham's circle algorithm
Posted on January 3, 2010
(Last modified on August 7, 2020)
| 2 minutes
| Kinshuk Chandra
Circles have the property of being highly symmetrical, which is handy when it comes to drawing them on a display screen.
Consider the diagram below:
|y | \\ ..... / . | . . \\ | / . . \\|/ . \--.---+---.-- . /|\\ . x . / | \\ . . | . / ..... \\ | | ```(This diagram is supposed to be a circle, try viewing it in 50 line mode).
[Read More]Line Drawing Algorithms
Posted on January 3, 2010
(Last modified on August 7, 2020)
| 2 minutes
| Kinshuk Chandra
Line Algorithm
The basic algorithm works for lines which look like this:
o------- | p1 -------- | deltaY ------- p2 | -------o | ---------------------------------- deltaX where p1 = (x1,y1), p2 = (x2, y2), x and y are both increasing from p1 to p2, deltaX = x2 - x1, deltaY = y2 - y1 and deltaX >= deltay. All other types of lines can be derived from this type. I’ll get to this bit later.
[Read More]Routine to draw a circle (x ** 2 + y ** 2 = r ** 2) without making use of any floating point computations at all.
Posted on January 3, 2010
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
Problem Write a routine to draw a circle (x ** 2 + y ** 2 = r ** 2) without making use of any floating point computations at all.
Solution Let
(x ^ 2 + y ^2 = r ^ 2)……………………………..1
The basic idea is to draw one quadrant and replicate it to other four quadrants.
Assuming the center is given as (a,b) and radius as r units, then start X from (a+r) down to (a) and start Y from (b) up to (b+r).
[Read More]Reversing a number
Posted on January 3, 2010
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
int n=12345,m=0;
cout«"Orginal No:“<
while(n>0)
m *= 10;
n /= 10;
cout«”\nRevesed No:“<
Recursive function
int reverse\_num(int n,int m)
{
if(n==0)
return m; //base (exit condition)
m\*=10;
m+=n%10;
return reverse\_num(n/10,m); //recursive call
}
```
To reverse the bits in an integer
Posted on January 3, 2010
(Last modified on August 7, 2020)
| 2 minutes
| Kinshuk Chandra
Method1
`
unsigned int num; // Reverse the bits in this number.
unsigned int temp = num; // temp will have the reversed bits of num.
int i;
for (i = (sizeof(num)*8-1); i; i–)
{
temp = temp | (num & 1);
temp «= 1;
num »= 1;
}
temp = temp | (num & 1);
`
Method2
In this method, we use a lookup table.
`
const unsigned char ReverseLookupTable[] =
[Read More]Check if the 20th bit of a 32 bit integer is on or off?
Posted on January 3, 2010
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
AND it with x00001000 and check if its equal to x00001000
if((num & x00001000)==x00001000)
Note that the digits represented here are in hex.
`
0 0 0 0 1 0 0 0
^
|
x0000 0000 0000 0000 0001 0000 0000 0000 = 32 bits
^ ^ ^
| | |
0th bit 20th bit 32nd bit
`
Count bits set in an integer
Posted on January 3, 2010
(Last modified on August 7, 2020)
| 6 minutes
| Kinshuk Chandra
Question: Write a function that determines the number of bits set to 1 in the binary representation of an integer.
Method 1 - Simple method (Right shift integer until it becomes 0)
Going by the brute force approach, it would be easy to come up with the following solution. If the least significant bit of a number is 1, it will return a result of 1 when we AND it with 1.
[Read More]c program without main
Posted on December 12, 2009
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
Basic Interview Questions on networking
Posted on December 9, 2009
(Last modified on August 7, 2020)
| 3 minutes
| Kinshuk Chandra
What is DHCP?
DHCP stands for Dynamic Host Configuration Technology. The basic purpose of the DHCP is to assign the IP addresses and the other network configuration such as DNS, Gateway and other network settings to the client computers. DHCP reduces the administrative task of manually assigning the IP addresses to the large number of the computers in a network.
What is DNS and how it works?
DNS stands for Domain name system and it translates (converts) the host name into the IP address and IP address into to the host name.
[Read More]