#include <bits/stdc++.h>
using namespace std;
const int N = 70;
const int MOD = 1000000007;
long long n, t, dp[N][N];
int p = 0;
long long dfs(int l, int c, int limit, long long n) {
  if (l < 0) return c == p;
  if (!limit && dp[l][c] != -1) return dp[l][c];
  int up = limit ? ((n >> l) & 1) : 1;
  long long ret = 0;
  for (int i = 0; i <= up; i++) {
    ret += dfs(l - 1, c + i, limit && i == up, n);
  }
  if (!limit) dp[l][c] = ret;
  return ret;
}
long long gao(long long n) {
  int t = 0;
  while ((1LL << t) < n) t++;
  return dfs(t, 0, 1, n);
}
int main() {
  cin >> n >> t;
  if ((t & (-t)) != t) {
    puts("0");
    return 0;
  }
  while ((1LL << p) < t) p++;
  p++;
  memset(dp, -1, sizeof(dp));
  cout << gao(n + 1) - gao(1) << endl;
  return 0;
}
