Ad

Showing posts with label Manipulate bits. Show all posts
Showing posts with label Manipulate bits. Show all posts

Saturday, 13 July 2013

Algo#43: Write basic function to implement atoi - Alpha to Integer

Function atoi is used to convert alphabetical string to integer. We can parse given string one character at a time, and convert it to integer. To make conversion fast enough, we can use bits manipulation for faster calculation.

Following is implementation of above problem in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Friday, 12 July 2013

Algo#42: Multiply by 7

Multiply given number by 7 without using * operator. What we can do is, multiply given number by 8 and then subtract given number from result. Now problem reduce to multiply by 8 rather than 7. But we know to multiply any number with power of 2, we can use shift operator. 

Following is implementation of above problem in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Friday, 5 July 2013

Algo#35: Check whether given number is power of two

To check whether given number is power of two, is problem of doing bits manipulation.

Following is implementation of above problem in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Saturday, 29 June 2013

Algo#29: Check whether given number's bit representation is palindrome or not.

To find whether anything is palindrome or not,easiest method is to reverse it and check whether it is same as original. We will follow same approach for checking whether given number's bit representation is palindrome or not.

Following is implementation of above algorithm in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Thursday, 27 June 2013

Algo#27: Count number of 1's in given number

Brute force method to count number of 1's in given number is to iterate through each bit and check if it is 1, then increment counter. But it will take 32 checks (assuming 32 bit number).

Can we do better? Is there any way by which we don't need to check all bits? What if we iterate as many times as numbers of 1's. To achieve it, we have to remove 1 from number at each iteration, then only at last when no one is remaining in number, we can stop.

Manipulation of bits in number is only solution to reduce iteration. By doing x-1, we can eliminate last 1 from given number but it will introduce tailing 1's in number. But those should be not an issue for us. Because we have AND weapon to get rid of all tail 1's.

Following is implementation of above algorithm in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Saturday, 22 June 2013

Algo#22: Swap nibbles of number.

Nibbles are nothing but chunk of 4 bits in simple language.

To swap nibbles in given number, we have to extract 4-bits chunk from number and depending on system, we have to shift them accordingly.

Following is implementation of above algorithm in c language for 8-bit system ( Imaginary ).





Please write comments if you find anything wrong or you want to add something more related to this topic.

Friday, 21 June 2013

Algo#21: Give next higher boundary number.

To give next higher boundary of number, is used when allocating memory to structure, so that access to its members are fast.


Following is implementation of above algorithm in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Wednesday, 19 June 2013

Algo#19: Convert given number in different endian system than your system.

There are mainly two types of system based on endianness available to us.
1) Big endian
2) Little endian

To convert one number other endianness system, we have to change order of all bytes.

Following is implementation of above algorithm in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Tuesday, 18 June 2013

Algo#18: Add two numbers without using plus(+) operator.

There are many ways to do one thing. Given problem very well follows it. This kind of problems will test how much comfortable you are with concepts as well as language. Because both will improve your skills to solve problem in more than one way.

There is no single algorithm for such tricks. Practice makes (wo)man perfect.

Following is implementation of some of the tricks in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Friday, 14 June 2013

Algo#14: Set specified bits range in number.

Set specified bits range in number.

Following is simple algorithm for setting bits range in given number.
Step 1: Make mask for start index to end index.
        1.1: First step to make mask is, set 1 in each position from 0 to end index, call it mask-1.
        1.2: Then same way, set 1 in each position from 0 to start index, call  it mask-2.
        1.3: XOR mask-1 and mask-2, which will give us our desired mask having 1 set from start to end index.
Step 2: Do OR operation between given number and mask.

Following is implementation of above algorithm in c language in 1 line.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Ad