#include <bits/stdc++.h>
using namespace std;
const int logn = 44;
long long n, t, dp[logn + 1][logn + 1][2];
int bits = 0;
inline int bit(long long x, int p) { return (x >> p) & 1; }
int main() {
  cin >> n >> t, n++;
  for (; t != 1; t >>= 1) {
    if (t & 1) cout << 0 << endl, exit(0);
    bits++;
  }
  bits++;
  dp[0][0][1] = 1;
  for (int i = 0; i < logn; i++)
    for (int j = 0; j <= i; j++)
      for (int z = 0; z < 2; z++) {
        if (!dp[i][j][z]) continue;
        int b = bit(n, logn - 1 - i);
        for (int t = 0; t < 2; t++) {
          if (z && t > b) continue;
          int nz = (z && t == b) ? 1 : 0;
          dp[i + 1][j + t][nz] += dp[i][j][z];
        }
      }
  cout << dp[logn][bits][0] + dp[logn][bits][1] - (bits == 1 ? 1 : 0) << endl;
  return 0;
}
