#include <bits/stdc++.h>
using namespace std;
const int maxn = 70;
long long N, T, dn[maxn][maxn][2];
long long dfs(int node, int left, int less) {
  if (node == -1) return !left;
  long long &sum = dn[node][left][less];
  if (sum != -1) return sum;
  sum = 0;
  if ((less || N & (1ll << node)) && left) sum += dfs(node - 1, left - 1, less);
  sum += dfs(node - 1, left, !!(N & (1ll << node) | less));
  return sum;
}
int main() {
  scanf("%lld %lld", &N, &T);
  N++;
  int x = -1, last = 0;
  for (int i = 0; i <= 60; i++) {
    if (T == (1ll << i)) x = i;
    if (N & (1ll << i)) last = i;
  }
  if (x == -1) {
    printf("0\n");
    return 0;
  }
  memset(dn, -1, sizeof dn);
  printf("%lld\n", dfs(last, x + 1, 0) - (T == 1));
  return 0;
}
