#include <bits/stdc++.h>
using namespace std;
const unsigned int MODULO = 1000000007;
const int INF = 100000000;
template <class T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
long long cnt[60];
vector<int> digits(long long m) {
  vector<int> ret;
  while (m) {
    ret.push_back(m & 1);
    m >>= 1;
  }
  return ret;
}
void digit_dp(long long m) {
  vector<int> a = digits(m);
  long long lseq[60][60] = {}, gt[60][60] = {};
  lseq[0][0] = 1;
  for (int i = 0; i < (((int)(a).size())); i++)
    for (int j = 0; j < (59); j++) {
      for (int d = 0; d < (2); d++) {
        int nj = j + (d == 1);
        if (d > a[i]) {
          gt[i + 1][nj] += lseq[i][j] + gt[i][j];
        } else if (d < a[i]) {
          lseq[i + 1][nj] += lseq[i][j] + gt[i][j];
        } else {
          lseq[i + 1][nj] += lseq[i][j];
          gt[i + 1][nj] += gt[i][j];
        }
      }
    }
  --lseq[a.size()][0];
  --lseq[a.size()][1];
  memset(cnt, 0, sizeof(cnt));
  for (int i = 0; i < (60); i++) cnt[i] += lseq[((int)(a).size())][i];
}
int main() {
  long long n, t;
  while (cin >> n >> t) {
    long long c = 1, m = 1;
    while (t > c) c <<= 1, m++;
    if (t != c) {
      puts("0");
      break;
    }
    digit_dp(n + 1);
    cout << cnt[m] << endl;
  }
  return 0;
}
