#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void checkMax(T& a, const T& b) {
  if (a < b) a = b;
}
template <typename T>
inline void checkMin(T& a, const T& b) {
  if (a > b) a = b;
}
const int kMod = 1000000007;
template <typename U, typename V>
inline void addMod(U& x, const V y) {
  x += y;
  if (x >= kMod) {
    x -= kMod;
  }
}
template <int N>
struct CombinationTable {
  CombinationTable() { init(); }
  static long long table[N + 1][N + 1];
  static void init() {
    table[0][0] = 1;
    for (int i = 1; i <= N; ++i) {
      table[i][0] = table[i][i] = 1;
      for (int j = 1; j < i; ++j) {
        table[i][j] = table[i - 1][j] + table[i - 1][j - 1];
      }
    }
  }
};
template <int N>
long long CombinationTable<N>::table[N + 1][N + 1];
CombinationTable<40> comb_tbl;
long long n, t;
void go() {
  if ((t & -t) != t) {
    puts("0");
    return;
  }
  ++n;
  int tt = 1;
  while (t >>= 1) ++tt;
  int num_one;
  long long tmp = n;
  do {
    num_one += (tmp & 1);
  } while (tmp >>= 1);
  long long ans = (tt == num_one);
  for (int n___LINE__ = (50), i = 0; i < n___LINE__; ++i) {
    if (n & 1) {
      --num_one;
      if (num_one <= tt && i >= 0 && tt >= num_one && tt - num_one <= i) {
        ans += comb_tbl.table[i][tt - num_one];
      }
    }
    n >>= 1;
  }
  if (tt == 1) --ans;
  cout << ans << endl;
}
int main() {
  cin >> n >> t;
  go();
  return 0;
}
