Print binary representation of a decimal number
Posted on March 21, 2014
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
intPart »= 1; this is fr wat?
Unknown - Jul 6, 2014
intPart »= 1; this is fr wat?
Hi Chaitrali, sorry for the delayed response. This means division by 2.
In general, bit_arg»shift_arg shifts bits to of bit_arg shift_arg places to the right – equivalent to integer division by 2^shift_arg
Print binary representation of a decimal number
Posted on March 21, 2014
(Last modified on August 7, 2020)
| 4 minutes
| Kinshuk Chandra
Problem
Given a (decimal – e.g. 3.72) number that is passed in as a string, print the binary representation. If the number can not be represented accurately in binary, print “ERROR”.
Solution
This is equivalent to implement toBinaryString() method built in Java API.
Lets think about the IEEE-754 standard. In a 32-bit machine, the 1st bit is for sign, 2nd – 9th digits are for exponent and the rest are for mantissa.
[Read More]Set all bits of a number in a range equal to another number
Posted on March 21, 2014
(Last modified on August 7, 2020)
| 3 minutes
| Kinshuk Chandra
Problem
You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e.g., M becomes a substring of N located at i and starting at j).
**Example **
Consider the integer N, where we want to fit in M between bits i and j of N.
Input: N = 10000000000, M = 10101, i = 2, j = 6 Output: N = 10001010100
[Read More]Divide a number by 3 without using *, /, +, -, % operators
Posted on March 19, 2014
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
Problem
Divide a number by 3 without using *, /, +, -, % operators
Solution
Method 1 - Divide the number using bitwise operator
We have already discussed how to divide a number by 3 with the help of ‘+’ operator here. So, all we have to do is write a plus operator with the help of bitwise operator, which we have done here.
int add(int x, int y) {
int a, b;
do {
a = x & y;
b = x ^ y;
x = a << 1;
y = b;
} while (a);
return b;
}
int divideby3 (int num) {
int sum = 0;
while (num > 3) {
sum = add(num >> 2, sum);
num = add(num >> 2, num & 3);
}
if (num == 3)
sum = add(sum, 1);
return sum;
}
Add two numbers without using arithmetic operators
Posted on March 19, 2014
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
Problem
Write a function Add() that returns sum of two integers. The function should not use any of the arithmetic operators (+, ++, –, -, .. etc).
Solution
We can get carry by &(and) operator and bit by ^ (xor) operator. We have already seen, how we can use bit-wise operators to replace arithmetic operators i.e. implement arithmetic operators using bitwise operators.
Method 1 - Iterative method
int Add(int x, int y) { // Iterate till there is no carry while (y !
[Read More]Divide a number by 3 without using any of operators (%,/,*)
Posted on March 19, 2014
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
Problem
Divide a number by 3 without using any of operators (%,/,*)
Solution
Method 1
int divideby3 (int num) { int sum = 0; while (num > 3) { sum += num >> 2; num = (num >> 2) + (num & 3); } if (num == 3) ++sum; return sum; } **Dry running the above method : **
For example if your number is 10 then convert to binary
[Read More]Add 1 to a given number without arithmetic operators
Posted on March 19, 2014
(Last modified on August 7, 2020)
| 2 minutes
| Kinshuk Chandra
**Problem **
Write a program to add one to a given number. You are not allowed to use operators like ‘+’, ‘-’, ‘*’, ‘/’, ‘++’, ‘–’ …etc.
Examples
Input: 12 Output: 13
Input: 6 Output: 7
Solution
We can use bitwise operators to achieve this. Following are different methods to achieve same using bitwise operators.
Method 1 - Flipping the bits
To add 1 to a number x (say 0011000111), we need to flip all the bits after the rightmost 0 bit (we get 0011000000).
[Read More]Find Position of the only Set Bit
Posted on January 17, 2014
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
Problem: You are given a number having only one “1″ its binary representation,. You have to find position of it?
Solution:
First of all we will check if number is power of two, Beacuse then only its binary represenattion will contain only one “1″.
After that, start from rightmost bit and one by one check value of every bit. Following are steps:
Initialize two variables; i = 1 (for looping) and pos = 1 (to find position of set bit) Inside loop, do bitwise AND of i and number ‘N’.
[Read More]Checking if a bit is on or off without affecting other bits
Posted on January 17, 2014
(Last modified on August 7, 2020)
| 2 minutes
| Kinshuk Chandra
Let’s say that we have a bit string of 1101 and we want to know if the 2nd bit is on or off. Here is a way to do it:
int isAvailable (int bitString, int targetNum) { return bit\_string & (1 << targetNum); } bit_string: the bit string whose bits we are interested in. For example, 1101 and we are interested in the 2nd bit which is the bit in bold.
[Read More]Toggle a specified bit in a bit string without effecting other bits
Posted on January 17, 2014
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
For example, we have a bit string 0111 and you have to turn the left most bit on 0 -> 1. How can we do that? We’ll use XOR (^), left shift operation («) and the 0001 (1) bit string.
Shift the 1 bit of 0001 to the position of the bit you want to change. In our example, we need to toggle the 3th bit (first bit is at position 0, that’s why our target is at 3rd position not 4th).
[Read More]