Example #1
0
binary convertDecToBin(long long int x) // decimal -> binary converter
{
    binary result;

    if(x == 1)
    {
        result = initBinaryResult(1);
        result.setNumberAt(0,1);
        return result;
    }

    if(powerOf2(x))
    {
        int index = log2(x);
        result = initBinaryResult(index + 1);
        result.setNumberAt(index, 1);
        return result;
    }

    int sup = 1;
    int n = 0;
    while(sup < x)
    {
        sup = sup << 1;
        n++;
    }

    result = initBinaryResult(n);

    sup /= 2;
    n--;

    while(x != 0)
    {
        if(sup <= x)
        {
            x -= sup;
            result.setNumberAt(n, true);
        }
        else
        {
            result.setNumberAt(n, false);
        }
        sup /= 2;
        n--;
    }

    return result;
}
Example #2
0
void* operator new(size_t sz)
{
    // put alloc node in front of requested memory
    void* ptr = malloc(sz + sizeof(alloc_node));
    if (ptr) {
        if (Tracking) {
            Lock l(mutex);
            ++Allocs;
            Bytes += sz;
            ++sizes[powerOf2(sz)].count_;
            insert(new (ptr) alloc_node);
        }
        return static_cast<char*>(ptr) + sizeof(alloc_node);
    }
    else
        assert(0);
}
Example #3
0
int main() {
    printf("%d\n", powerOf2(-3));

    return 0;
}
Example #4
0
int main()
{
    int ans = powerOf2(4);
    return 0;
}