#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
int dblcmp(double d) {
  if (fabs(d) < eps) return 0;
  return d > eps ? 1 : -1;
}
long long n, t;
int a[66], l, p;
long long dp[66][2][66];
long long getdp(int len, int less, int tot) {
  if (len == l) {
    return (tot == p);
  }
  if (dp[len][less][tot] != -1) return dp[len][less][tot];
  long long res = 0;
  int u, i;
  if (less)
    u = 1;
  else
    u = a[len];
  for (i = 0; i <= u; i++) {
    res += getdp(len + 1, less || (i < a[len]), tot + i);
  }
  return dp[len][less][tot] = res;
}
long long gao(long long x) {
  l = 0;
  while (x) {
    a[l++] = x % 2;
    x /= 2;
  }
  reverse(a, a + l);
  memset((dp), -1, sizeof(dp));
  return getdp(0, 0, 0);
}
int main() {
  cin >> n >> t;
  if (t & (t - 1LL)) {
    puts("0");
    return 0;
  }
  int i, j, k;
  for (p = 0;; p++) {
    if (t == (1LL << p)) break;
  }
  p++;
  long long ans = gao(n + 1);
  if (t == 1) ans--;
  cout << ans << endl;
  return 0;
}
