I don't want someone to explain how the following code works (it checks whether an int is pandigital) as I should be doing that myself. I just need help understanding line 8 specifically. I don't know what the | is doing.
private bool isPandigital(long n) {
int digits = 0;
int count = 0;
int tmp;
while (n > 0) {
tmp = digits;
digits = digits | 1 << (int)((n % 10) - 1);
if (tmp == digits) {
return false;
}
count++;
n /= 10;
}
return digits == (1 << count) - 1;
}
I know others have already explained that it's a bitwise OR, but I'd like to give my own interpretation.
digits = digits | X will copy all the 1 bits from X into digits.
digits = digits | 1 << Y will "set" a single bit in digits - it will set the Yth bit.
So, each loop sets a bit in digits.
| is bitwise or. But the code checks whether an int of length n has all digits 1..n. This is different from palindrome check. That line sets's (i-1)'th bit of digits to 1 if the last digit of n is i. [BTW, the code is wrong: if n contains a zero-digit, that line will trigger "undefined behavior": shifting an integer by a negative amount gives an undefined result.]
The code uses an integer digits to represent a set of digits. You can learn more about the technique by searching for bit sets.
It appears to be performing a Bitwise Or.
| is a bitwise OR
A bitwise OR takes two bit patterns of equal length and performs the logical inclusive OR operation on each pair of corresponding bits. The result in each position is 1 if the first bit is 1 OR the second bit is 1 OR both bits are 1; otherwise, the result is 0.
Example:
10010000
01010000
--------
11010000
http://en.wikipedia.org/wiki/Bitwise_operation
| is a bitwise or.
So the line is doing digits = digits | (1 << (int)((n % 10) - 1));
Related
#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.
I'm being requested to write a C++ program that computes the n-th number that doesn't contain a given digit and the time execution to be lower than 0.1 seconds. Memory doesn't seem to be an issue, as I'm allowed the use of up to 64 MB.
The original text of the problem goes like this:
Cifra4
To represent numbers, it was decided not to use the digit C
again.
Thus, from the array of natural numbers, all numbers containing the
digit C will be erased. Let the new array be S.
Requirements
1) Determine the N-th number in S.
2) Y and Z are two natural
numbers from the array of all natural numbers. Determine the number of
natural numbers removed from Y to Z.
Input data
The input file cifra4.in contains the first number T representing the
type of requirement. If T == 1, the second row will contain
the digit C and the number N. If T == 2, the
second line will contain the digit C and two natural numbers Yand
Z.
Output data
In the output file cifra4.out will contain in the first row
one natural number according to the type of requirement.
Restrictions and clarifications
1 ≤ N ≤ 10 ^ 13
0 ≤ C ≤ 9
1 ≤ Y ≤ 10 ^ 13
1 ≤ Z ≤ 10 ^ 13
for 20% of the tests, N will have a maximum of 5 digits
for 20% of the tests, Y and Z will have a maximum of 6 digits
Example 1
cifra4.in
1
0 11
cifra4.out
12
Example 2
cifra4.in
2
1 3 20
cifra4.out
10
My best try was a code that determined (or at least was supposed to) the n-th number that doesn't contain the digit "0", but for 10 ^ 13 it returned 23210987654321, which obviously contains 0.
My slower, but correct approach was what I ended up keeping. Here is the code:
#include <fstream>
std::ifstream in("cifra4.in");
std::ofstream out("cifra4.out");
const long long pow_of_10[14] = {0, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000,
10000000000, 100000000000, 1000000000000};
void req_1 ()
{
short digit;
long long n;
in >> digit >> n;
for (long long i = 0; i <= n; i++)
{
long long nr = i;
if (nr)
{
long k = 1;
do
{
if (nr % 10 == digit)
{
n += pow_of_10[k];
i += pow_of_10[k] - 1;
break;
}
nr /= 10;
k++;
}
while (nr);
}
else if (digit == 0) n++;
}
out << n - 1;
}
void req_2()
{
short digit;
long long lhs, rhs;
long long elim = 0;
in >> digit >> lhs >> rhs;
for (long long i = lhs; i <= rhs; i++)
{
long long nr = i;
while (nr)
{
if (nr % 10 == digit)
{
elim++;
break;
}
nr /= 10;
}
}
out << elim;
}
int main()
{
short requirement;
in >> requirement;
if (requirement == 1)
req_1();
else
req_2();
}
NOTE
I'm not asking for code necesarily, but for ideas, possible algorithms that can execute up to 10 ^ 13 in decent time, preferably the time requested by the problem, but 1 second will do fine for me.
Imagine that 9 is the forbidden digit. In this case you can just convert your number to base-9 and you're done.
Now, what happens when the forbidden digit is different, say d? It's still a base-9 number but you have to map your digits, such that digits below d remain unaffected, and d and above are mapped to the digit d + 1.
For example, when the forbidden digit is 7 and n is 125.
Step 1: convert to base-9: 12510 = 1489
Step 2: map the digits. 1 → 1, 4 → 4, 8 → 9
The solution is 149.
Since the decimal digits of a number are "independent" of each other, in that setting one digit does not affect any other digits - once you've fixed a prefix of (at least one) more-significant digits, leaving n' less-significant digits, you know you have exactly (10 - 1)^n' = 9^n' numbers with that prefix and no forbidden digits in the unfixed part. For example, for 3-digit numbers beginning with 1, there are exactaly 81 numbers with no 0 in them.
The only 'snag' here is that setting the most-significant digit to zero means that you get the same number for different numbers of digits (012, 0012 etc.). But you should be able to work around this issue as well - by determining how many digits you're going to need for the n'th number without your forbidden digit. Very similar to the argument I described above. Then you know you have 10-1 = 9 options there if your forbidden digit is 0 or 10-2 = 8 otherwise.
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.
I have a problem wherein I need to find the number of matching bits (from left to right) between two integers
Inputs: 2 Variable A and B to store my decimal numbers
Output: Numbers of bits in A and B that match (starting from the left)
Some Examples:
A = 3 and B = 2, A and B bits match up to 7 bits from the left bit
A = 3 and B = 40, A and B bits match up to 7 bits from the left bit.
How can I do that using bitwise operation (AND,OR,XOR)?
Thanks
XOR the two together (to produce a number which has all zeroes from the left until the first non matching element), then shift the result right until it equals 0. Subtract this from the bit length of the integers you are dealing with (e.g., you seem to be implying 8 bits).
pseudocode:
int matchingBits(A, B) {
result = A XOR B
int shifts = 0
while (result != 0) {
result = result >> 1 (Shift right the result by 1)
shifts++
}
return integer_bit_length - shifts
}
Do (A XNOR B) to find the matching digits:
10101010
01001011
--XNOR--
00011101
Then use the hamming algorithm to count the ones: Count number of 1's in binary representation
(btw: xnor is !xor)
try this may be it will work
int matchingBitsCount(val1,val2)
{
int i , cnt = 0;
for(i=7;i>=0;i--)
{
if(((1<<i)&a)^((1<<i)&b))==0) //left shifted for starting from left side and then XOR
{
cnt++;
}
else
{
break;
}
}
}
i take val1 and val 2 as char if you want to check for int then just take i=31
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.