Question #242151

Explain the methodologies of various type that can be used to manipulate bits in register. Elaborate with proper examples.

Expert's answer

- Considering the number N and you need to find if N is a power of 2. Simple solution to this problem is to repeated divide N by 2 if N is even. If we end up with a 1 then N is power of 2, otherwise not. There are a special case also. If N = 0 then it is not a power of 2

2.Â Â Â Â Â Â **Counting the number of ones in the binary representation of the given number.**

The basic approach to evaluate the binary form of a number is to traverse on it and count the number of ones. But this approach takes log_{2}N of time in every case.

Why log_{2}N ?

As to get a number in its binary form, we have to divide it by 2, until it gets 0, which will take log_{2}N of time.

3.Â Â Â Â Â Checking **if the i**^{th}**Â bit is set in the binary form of the given number.**

To check if the i^{th}Â bit is set or not (1 or not), we can use AND operator. For instance, assuming that we have a number N, and to check whether itâ€™s i^{th}Â bit is set or not, we can AND it with the number 2^{i}Â . The binary form of 2^{i}Â contains only i^{th}Â bit as set (or 1), else every bit is 0 there. When we will AND it with N, and if the i^{th}Â bit of N is set, then it will return a non zero number (2^{i}Â to be specific), else 0 will be returned.

Â

Learn more about our help with Assignments: Electrical Engineering

## Comments

## Leave a comment