#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const double EPS = 1e-9, INF = 1e12;
int m, d[70];
long long dp[70][70][2];
int main() {
  long long n, t;
  cin >> n >> t;
  n++;
  for (; n; n /= 2) d[m++] = n % 2;
  reverse(d, d + m);
  int bit = __builtin_ctzll(t) + 1;
  if (__builtin_popcountll(t) != 1) {
    cout << 0 << endl;
    return 0;
  }
  dp[0][0][0] = 1;
  for (int i = 0; i < (int)m; i++)
    for (int j = 0; j < (int)i + 1; j++)
      for (int k = 0; k < (int)2; k++)
        if (dp[i][j][k]) {
          for (int l = 0; l < (int)2; l++) {
            int nk = k || l < d[i];
            if (!nk && l > d[i]) continue;
            dp[i + 1][j + l][nk] += dp[i][j][k];
          }
        }
  cout << dp[m][bit][0] + dp[m][bit][1] - (t == 1) << endl;
  return 0;
}
