K
K
kate2019-01-20 01:45:29
C++ / C#
kate, 2019-01-20 01:45:29

How are these transactions explained?

This is a binary exponentiation, I found a function, but I can not understand the operations "--n", "n >>= 1". With the latter, it's kind of like a shift to the right, but why is the equal sign used? Probably a stupid question, but I don't really understand. And how does (n&1) work. If these are bit operations, but we do not convert the decimal number to the binary system. How does it work? :about

int binpow (int a, int n)
{
  int res = 1;
  while (n)
    if (n & 1)
    {
      res *= a;
      --n;
    }
    else 
    {
      a *= a;
      n >>= 1;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Armenian Radio, 2019-01-20
@kkate4ka

--n - short notation for n=n-1
n >>= 1 - short notation for n=n>>1
"If these are bit operations, but we do not convert decimal to binary" - you are mistaken. The computer just translates all the numbers into the binary system and stores them in the binary system.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question