#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000;
long long num[MAXN][MAXN];
long long n, t;
long long cal(long long isum, long long i) {
  long long ret = 0;
  long long x = n - (isum - i);
  vector<int> v;
  for (int k = 0; x; ++k)
    if (x & (1LL << k)) v.push_back(k), x -= (1LL << k);
  for (int j = 0; j < v.size(); ++j)
    for (int k = 0; k <= v[j]; ++k)
      if (1LL << (k + v.size() - j - 1) == t) ret += num[v[j]][k];
  return ret;
}
long long cal2(long long tt) {
  long long ret = 0;
  for (int j = 0; j <= tt; ++j) {
    if (j)
      num[tt][j] = num[tt - 1][j] + num[tt - 1][j - 1];
    else
      num[tt][0] = 1;
    if (1LL << j == t) ret += num[tt][j];
  }
  return ret;
}
int main() {
  cin >> n >> t;
  if (t & (t - 1))
    cout << 0 << endl;
  else {
    memset(num, 0, sizeof(num));
    num[0][0] = 1;
    long long isum = 0, i, ans = 0, tt = 1;
    for (i = 2; isum <= n; i <<= 1, ++tt) {
      isum += i;
      if (isum > n)
        ans += cal(isum, i);
      else
        ans += cal2(tt);
    }
    cout << ans << endl;
  }
  return 0;
}
