how to count only 1bits in binary representation of number without loop - c++

Suppose I have two numbers(minimum and maximum) . `
for example (0 and 9999999999)
maximum could be so huge. now I also have some other number. it could be between those minimum and maximum number. Let's say 15. now What I need to do is get all the multiples of 15(15,30,45 and so on, until it reaches the maximum number). and for each these numbers, I have to count how many 1 bits there are in their binary representations. for example, 15 has 4(because it has only 4 1bits).
The problem is, I need a loop in a loop to get the result. first loop is to get all multiples of that specific number(in our example it was 15) and then for each multiple, i need another loop to count only 1bits. My solution takes so much time. Here is how I do it.
unsigned long long int min = 0;
unsigned long long int max = 99999999;
unsigned long long int other_num = 15;
unsigned long long int count = 0;
unsigned long long int other_num_helper = other_num;
while(true){
if(other_num_helper > max) break;
for(int i=0;i<sizeof(int)*4;i++){
int buff = other_num_helper & 1<<i;
if(buff != 0) count++; //if bit is not 0 and is anything else, then it's 1bits.
}
other_num_helper+=other_num;
}
cout<<count<<endl;

Look at the bit patterns for the numbers between 0 and 2^3
000
001
010
011
100
101
110
111
What do you see?
Every bit is one 4 times.
If you generalize, you find that the numbers between 0 and 2^n have n*2^(n-1) bits set in total.
I am sure you can extend this reasoning for arbitrary bounds.

Here's how I do it for a 32 bit number.
std::uint16_t bitcount(
std::uint32_t n
)
{
register std::uint16_t reg;
reg = n - ((n >> 1) & 033333333333)
- ((n >> 2) & 011111111111);
return ((reg + (reg >> 3)) & 030707070707) % 63;
}
And the supporting comments from the program:
Consider a 3 bit number as being 4a + 2b + c. If we shift it right 1 bit, we have 2a + b. Subtracting this from the original gives 2a + b + c. If we right-shift the original 3-bit number by two bits, we get a, and so with another subtraction we have a + b + c, which is the number of bits in the original number.
The first assignment statement in the routine computes 'reg'. Each digit in the octal representation is simply the number of 1’s in the corresponding three bit positions in 'n'.
The last return statement sums these octal digits to produce the final answer. The key idea is to add adjacent pairs of octal digits together and then compute the remainder modulus 63.
This is accomplished by right-shifting 'reg' by three bits, adding it to 'reg' itself and ANDing with a suitable mask. This yields a number in which groups of six adjacent bits (starting from the LSB) contain the number of 1’s among those six positions in n. This number modulo 63 yields the final answer. For 64-bit numbers, we would have to add triples of octal digits and use modulus 1023.

Related

Have some doubts in the operation [Bitwise Manipulation]

#include <iostream>
#include <math.h>
using namespace std;
// converting from decimal to binary
int main()
{
int n, bit, ans = 0, i = 0;
cout << "enter a number: ";
cin >> n;
while (n > 0)
{
bit = n & 1;
ans = round(bit * pow(10, i)) + ans; /* we used 'round' bcoz pow(10, 2) give 99.99999
it differs from compiler to compiler */
n = n >> 1;
i++;
}
cout << ans;
return 0;
}
I am unable to understand that in while(n>0), n will be stored as binary form or decimal form.
That is if n=5, so whether while loop check for (5>0) or (101>0).
Can anyone explain what is happening here?
I am new to this platform, please don't delete my question. My earlier questions are also gets deleted due enough dislikes. I am here to learn and am still learning.
"Decimal" and "Binary" are text representations of a value. n is an int, not text, so it holds the value 5. If I have 5 apples on a desk, then there is no "decimal" or "binary" involved. THere's just 5 apples. Same with int.
cin >> n;
while (n > 0)
This loop continues because 5 > 0.
n = n >> 1;
This is just a fancy way of writing n = n / 2, so then n becomes 2. Since 2 > 0, the loop continues. On the third run, n becomes 1, and since 1 > 0, the loop continues a third time. On the fourth run, n becomes 0, and 0 == 0, so the while loop exits.
Both n and ans are integers that your computer stores in a binary format. If n = 5 then n is both 5 and 0b0101. Your loop converts one integer n into a second integer ans which only uses the digits 1 and 0 and looks like the integer n in binary.
It does this by converting each power of two in n into a power of ten and adding it into ans. The integer 5 will become the integer 101 (one hundred and one).
In the loop, when you use the bitwise operator >> you are manipulating the underlying binary representation of the number directly, in this case, shifting all of the bits in the integer to the right and feeding in zeros on the left to replace the bits that have moved.
So, if n is 5 then:
0b0101 >> 1 gives 0b0010, or 2
0b0101 >> 2 gives 0b0001, or 1
0b0101 >> 3 gives 0b0000, or 0
When you use the bitwise operator & (bitwise and) you are again operating directly on the binary representation. This time you are and-ing all of the bits in one number with the bit in another number. For example:
0b01100110 & 0b10101110 = 0b00100110
0b01100110 & 0b10011001 = 0b00000000
In your loop you are doing four things:
1. bit = n & 1
And-ing the integer n with one will mean that bit is equal to:
1 when there is a 1 in the 2^0 place (the least significant bit) and,
0 when there is not a 1 in the 2^0 place.
2. ans = round(bit * pow(10, i)) + ans
You take 10^i and multiply it by bit. So:
bit is zero this is zero
if bit is one this is some power of ten.
3. n = n >> 1
Shift one place to the right. The bit that was in the 2^1 place is now in the 2^0 place.
4. i++
Increment i which tracks both your current power of 2 and current power of ten.
#Mooing Duck has explained it incredibly well. I'd just add that you are being confused about the Number System
Decimal numbers are called the base 10 numbers (digits: 0 - 9, total: 10)
Binary numbers are called the base 2 numbers(digits/bits: 0 - 1, total: 2)
101 (base 10) is very different from 101 (base 2)
When we use bitwise operators such as (<<, >>, &, ^, |) we manipulate the bits of the decimal (hence the name bitwise operators)
So, when you are doing 5 >> 1 you are actually doing 101 (base 2) >> 1 which results in 010 (base 2)
When you keep shifting the bits to the right(equivalent to dividing by 2), at one point you'll be left with no 1s and only 0s. And what's 0 (base 2)? Its 0(base 10). Hence, the loop breaks.

Required: large number (max 1000 digits) stored in string modulo 11

I have a question, which is to find the modulo 11 of a large number. The number is stored in a string whose maximum length is 1000. I want to code it in c++. How should i go about it?
I tried doing it with long long int, but its impossible that it can handle the corner case value.
A number written in decimal positional system as a_na_{n-1}...a_0 is the number
a_n*10^n+a_{n-1}*10^{n-1}+...+a_0
Note first that this number and the number
a_0-a_{1}+a_{2}+...+(-1)^{n}a_n
which is the sum of its digits with alternating signs have the same remainder after division by 11. You can check that by subtracting both numbers and noting that the result is a multiple of 11.
Based on this, if you are given a string consisting of the decimal representation of a number, then you can compute the remainder modulo 11 like this:
int remainder11(const std::string& s) {
int result{0};
bool even{true};
for (int i = s.length() - 1; i > -1; --i) {
result += (even ? 1 : -1) * ((int)(s[i] - '0'));
even = !even;
}
return ((result % 11) + 11) % 11;
}
Ok, here is the magic (math) trick.
First imagine you have a decimal number that consists only of 1s.
Say 111111, for example. It is obvious that 111111 % 11 is 0. (Since you can always write it as the sum of a series of 11*10^n). This can be generalized to all integers consists purely of even numbers of ones. (e.g. 11, 1111, 11111111). For those with odd number of ones, just subtract one from it and you will get a 10 times some number that consists of odd numbers of one (e.g 111=1+11*10), so their modulo to 11 would be 1.
A decimal number can be always written as the form of
where a0 is the least significant digit and an is the most significant digit. Note that 10^n can be written as 10^n - 1 + 1, and 10^n - 1 is a number consists of n nines. If n is even, then you will get 9 times some even number of ones, and its modulo to 11 is always 0. If n is odd, then we get 9 times some odd number of ones, and its modulo to 11 is always 9. And don't forget we've still got a +1 after 10^n - 1 + 1 so we need to add a to the result.
We are very close to our results now: we just have to add things up and do a final modulo to 11. The pseudo-code would be like:
Initialize sum to 0.
Initialize index to 0.
For every digit d from the least to most significant:
If the index is even, sum += d
Otherwise, sum += 10 * d
++index
sum %= 11
Return sum % 11

Counting Minimum Number of Bytes Required to Represent a Signed Integer

My task seems simple, I need to calculate the minimum number of bytes required to represent a variable integer (for example if the integer is 5, then I would like to return 1; if the integer is 300, I would like to return 2). Not referring to the data type int which is, as pointed out in comments, always just sizeof(int), I'm referring to a mathematical integer. And I almost have a solution. Here is my code:
int i;
cin >> i;
int length = 0;
while (i != 0) {
i >>= 8;
length++;
}
The problem is that this doesn’t work for any negative numbers (I have not been able to determine why) or some positive numbers where the most significant bit is a 0 (because the sign bit is the bit that makes it one bit larger)... Is there any hints or advice I can get in how to account for those cases?
Stored as a single byte,
Positive numbers are in the range 0x00 to 0x7F
Negative numbers are in the range 0x80 to 0xFF
As 2-bytes,
Positive numbers are in the range 0x0000 to 0x7FFF
Negative numbers are in the range 0x8000 to 0xFFFF
As 4-bytes,
Positive numbers are in the range 0x00000000 to 0x7FFFFFFF
Negative numbers are in the range 0x80000000 to 0xFFFFFFFF
You can use a function like the following to get the minimum size:
int getmin(int64_t i)
{
if(i == (int8_t)(i & 0xFF))
return 1;
if(i == (int16_t)(i & 0xFFFF))
return 2;
if(i == (int32_t)(i & 0xFFFFFFFF))
return 4;
return 8;
}
Then for example, when you see 0x80, translate it to -128. While 7F is translated to 127, and 0x801 should be translated to a positive number.
Note that this will be very difficult and rather pointless, it should be avoided. This doesn't accommodate storage of numbers in triple bytes, for that, you have to make up your own format.
The range of signed numbers possible to store in x bytes in 2's complement is -2^(8*x-1) to 2^(8*x-1)-1. For example, 1 byte can store signed integers from -128 to 127. Your example would incorrectly calculate that only 1 byte is needed to represent 128 (if we are talking about signed numbers), as right shifting by 8 would equal zero, but that last byte is required to know that this is not a negative number.
For handling negatives, turning it into a positive number and subtracting one (because negative numbers can store an extra value) will allow you to right shift.
int i;
cin >> i;
unsigned bytes = 1;
unsigned max = 128;
if (i < 0) {
i = ~i; //-1*i - 1
}
while(max <= i) {
i >>= 8;
bytes++;
}
cout << bytes;
Another option is to use __builtin_clz() if you are using gcc. This returns the leading zeros, which you can then use to determine the minimum number of bytes.

MSB aligned addition

I'm looking for a way (preferably recursive) to add two integers with their msb aligned.
For example: 125 + 25 = 375.
I tried to reverse the digits to effectively align them but the carrying would be all wrong. ie. 526 (625) + 05 (50) = 531.
1) Calculate number of digits of both numbers using a while / 10 loop
2) Get the difference
3) Multiply smallest number by 10 ^ difference
4) Add them together
you will need to include math.h for this. Assuming m and n are natural numbers, the below works by multiplying the smaller number by 10 (if needed) until it has the same number of digits as the larger, then adding.
int funkyAdd (int m, int n)
{
if ((m<=0)||(n<=0)){return -1;}
int smaller=std::min(m,n);
int larger=std::max(m,n);
while (floor(log10(smaller))<floor(log10(larger))){smaller*=10;};
return (smaller+larger);
}

Find count of overlapping 1 bits

I'm trying to find the number of overlapping 1 bits between 2 given numbers.
For example, given 5 and 6:
5 // 101
6 // 110
There is 1 overlapping 1 bit (the first bit)
I have following code
#include <iostream>
using namespace std;
int main() {
int a,b;
int count = 0;
cin >> a >> b;
while (a & b != 0) {
count++;
a >>= 1;
b >>= 1;
}
cout << count << endl;
return 0;
}
But when I entered 335 and 123 it returned 7 but I think it is not correct
Can someone see a problem with my code?
The problem is that you're just printing out the number of times any of the bits match, as you lop off the least significant bit for each iteration (which will at max be the number of bits set for the smaller number). You're comparing all bits of [a] BITWISE AND [b] each iteration. You could rectify this by masking with 1: a & b & 1, so that while you're shift thing bits rightward each time, you're only checking if the least significant bit is being checked:
while (a && b){
count += a & b & 1;
a>>=1;
b>>=1;
}
Your existing algorithm counts each bit as long as any bit in the remaining bits to test matches; since 123 and 335 have a common MSB, it's true as long as either number is non-zero. 123 is the smaller with 7 bits, so it's true 7 times until that number is completely processed. As an extreme case, 128 (10000000) and 255 (11111111) would return 8 with your method, even though it's actually 1.
You want to AND the two numbers together to start with and then count the number of 1s in that result
You want to count the number of bits that are set. Instead, your code is sort of computing the binary logarithm.
Only increment the count if the lowest order bit is set.
for (int c = a & b; c != 0; c >>= 1) {
if (c & 1)
++count;
}
Slightly shorter form:
int countCommonBits(int a,int b) {
int n = 0;
for (unsigned v = (unsigned)(a & b); v; v >>= 1) {
n += 1 & v;
}
return n;
}
If you know both numbers are positive, you can omit the use of the unsigned type. Note when using "int" that sign extension on a right shift of a negative number would give you a bit of an overcount (i.e. an infinite loop).
Much later...
Reviewing an old answer, came across this. The current "accepted" answer is 1) inefficient, and 2) an infinite loop if the numbers are negative. FWIW.