#include <bits/stdc++.h>
long long c[41][41];
long long n, t, sol;
int main(void) {
  scanf("%I64D%I64d", &n, &t);
  if (t & (t - 1)) {
    printf("0\n");
    return 0;
  }
  for (int i = 0; i <= 40; i++) {
    c[i][0] = 1;
    for (int j = 1; j <= i; j++) c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
  }
  int pot = 0;
  for (; (1ll << pot) < t; pot++)
    ;
  pot++;
  n += 2;
  int k = 0;
  for (int i = 40; i >= 0 && k <= pot; i--)
    if ((1ll << i) & n) {
      sol += c[i][pot - k];
      k++;
    }
  if (pot == 1) sol--;
  printf("%I64d\n", sol);
  return 0;
}
