#include <bits/stdc++.h>
using namespace std;
long long c;
vector<int> a;
int N;
long long C[65][65];
long long nCk(long long n, long long k) {
  if (k > n) return 0;
  return C[n][k];
}
long long f(int index, int onesNeeded, int tight) {
  if (index == N) {
    if (onesNeeded == 0)
      return 1;
    else
      return 0;
  }
  if (a[index] == 0) {
    return f(index + 1, onesNeeded, tight);
  } else {
    long long ans = nCk(N - index - 1, onesNeeded);
    if (onesNeeded >= 1) ans += f(index + 1, onesNeeded - 1, tight);
    return ans;
  }
}
int brute(int n) {
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int t = i, cnt = 0;
    while (t) {
      if (t % 2 == 1) cnt++;
      t /= 2;
    }
    if (cnt == 5) ans++;
  }
  return ans;
}
int main() {
  for (int i = 0; i <= 60; i++) C[i][0] = 1, C[i][i] = 1;
  for (int n = 1; n <= 60; n++) {
    for (int r = 1; r <= n; r++) {
      C[n][r] = C[n - 1][r] + C[n - 1][r - 1];
    }
  }
  long long n, t;
  cin >> n >> t;
  n++;
  int ok = 0;
  if ((t & (t - 1)) == 0) ok = 1;
  while (t) {
    t /= 2;
    c++;
  };
  if (!ok) {
    cout << 0 << endl;
    return 0;
  }
  while (n) {
    a.push_back(n % 2);
    n /= 2;
  }
  reverse(a.begin(), a.end());
  N = a.size();
  long long ans1 = f(0, c, 1);
  if (c == 1) ans1--;
  cout << ans1 << endl;
  return 0;
}
