#include <bits/stdc++.h>
using namespace std;
template <typename T>
T in() {
  char ch;
  T n = 0;
  bool ng = false;
  while (1) {
    ch = getchar();
    if (ch == '-') {
      ng = true;
      ch = getchar();
      break;
    }
    if (ch >= '0' && ch <= '9') break;
  }
  while (1) {
    n = n * 10 + (ch - '0');
    ch = getchar();
    if (ch < '0' || ch > '9') break;
  }
  return (ng ? -n : n);
}
long long n, t, ar[101 + 5][101 + 5];
long long BruteForce(long long m) {
  memset(ar, 0, sizeof(ar));
  int i, j, k;
  long long ret = 0LL;
  for (i = 1; i <= m; i++) ar[1][i] = 0;
  ar[1][m + 1] = 1;
  for (i = 2; i <= m + 1; i++) {
    for (j = 1; j <= m + 1; j++) {
      if (j == 1)
        ar[i][j] = ar[i - 1][j + 1];
      else if (j == m + 1)
        ar[i][j] = ar[i - 1][j - 1];
      else
        ar[i][j] = ar[i - 1][j - 1] ^ ar[i - 1][j + 1];
    }
  }
  for (j = 1; j <= m + 1; j++) ret += ar[m + 1][j];
  cout << m << " : " << ret << endl;
  return ret;
}
int m, b[65];
long long dp[65][2][65];
long long go(int p, int ch, int on) {
  if (p == m) return (!on);
  if (on < 0) return 0;
  long long &ret = dp[p][ch][on];
  if (ret != -1) return ret;
  ret = 0LL;
  int mx = b[p];
  if (ch) mx = 1;
  for (int i = 0; i <= mx; i++) {
    ret += go(p + 1, ch || (i < mx), on - (i == 1));
  }
  return ret;
}
void DecToBin(long long nm) {
  m = 0;
  while (nm) {
    b[m++] = (nm % 2LL);
    nm /= 2LL;
  }
  reverse(b, b + m);
}
int main() {
  int j, k;
  long long i;
  scanf("%I64d%I64d", &n, &t);
  if (t & (t - 1)) {
    printf("0\n");
    return 0;
  }
  int dgt = log2(t);
  dgt++;
  memset(dp, -1, sizeof(dp));
  n++;
  DecToBin(n);
  long long ans = go(0, 0, dgt);
  if (dgt == 1) ans--;
  printf("%I64d\n", ans);
  return 0;
}
