#include <bits/stdc++.h>
using namespace std;
const int N = 1100;
long long int ans;
long long int d[N][N];
void dfs(int ct, long long int n, int cnt) {
  if (ct == 1) {
    if (cnt == 0) ans++;
    return;
  }
  long long int all = (1ll << (ct - 2));
  if (n > all) {
    ans += d[ct - 1][cnt];
    dfs(ct - 1, n - (1ll << (ct - 2)), --cnt);
  } else
    dfs(ct - 1, n, cnt);
}
int main() {
  long long int n, t;
  scanf("%lld%lld", &n, &t);
  int ct(0), cnt(0);
  for (long long int o(n + 1); o; o >>= 1, ++ct)
    ;
  for (long long int o(t); o; o >>= 1, ++cnt)
    ;
  --cnt;
  if (t != (1 << cnt)) {
    printf("0");
    return 0;
  }
  d[1][0] = 1;
  for (int i = 2; i <= ct; ++i) {
    d[i][0] = 1;
    for (int j = 1; j < i; ++j) d[i][j] = d[i - 1][j] + d[i - 1][j - 1];
  }
  for (int i = 2; i < ct; ++i) ans += d[i][cnt], n -= (1ll << (i - 1));
  dfs(ct, n, cnt);
  printf("%lld", ans);
  return 0;
}
