dstamatova

BinaryToDecimal

Nov 26th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.41 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // Function to convert binary to decimal
  5. int binaryToDecimal(int binNumber)
  6. {
  7. int decValue = 0;
  8. int base = 1; //основа - 2^0=1
  9. while (binNumber)
  10. {
  11. decValue += (binNumber & 1) * base;
  12. binNumber >>= 1;
  13. base *= 2;
  14. }
  15. return decValue;
  16. }
  17.  
  18. // Driver program to test above function
  19. int main()
  20. {
  21. cout << binaryToDecimal(0b10000000000) << endl;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment