Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <random>
- using namespace std;
- int main(){
- int array[8];
- int power = 1;
- int powered_num = 1;
- int decimal_number = 0;
- //это цикл, который генерирует двоичное число(массив 1 и 0)
- //и превращает его в десятичное число
- //00010011 = 19
- for (int i = 0; i < 8; i++){
- array[i] = rand()%2;
- cout << array[i];
- //внутренний цикл считает степень, в которую нужно возвести
- // i-й эл-т массива
- for (int j = 0; j < i; j++){
- powered_num *= 2;
- }
- array[i] *= powered_num;
- decimal_number += array[i];
- powered_num = 1;
- }
- cout << "\nDecimal number is: " << decimal_number << "\n\n";
- decimal_number = 0;
- //11111100 = 271
- for (int i = 0; i < 8; i++){
- array[i] = rand()%2;
- cout << array[i];
- for (int j = 0; j < i; j++){
- powered_num *= 2;
- }
- array[i] *= powered_num;
- decimal_number += array[i];
- powered_num = 1;
- }
- cout << "\nDecimal number is: " << decimal_number << "\n\n";
- decimal_number = 0;
- //10010101 = 149
- for (int i = 0; i < 8; i++){
- array[i] = rand()%2;
- cout << array[i];
- for (int j = 0; j < i; j++){
- powered_num *= 2;
- }
- array[i] *= powered_num;
- decimal_number += array[i];
- powered_num = 1;
- }
- cout << "\nDecimal number is: " << decimal_number << "\n\n";
- }
- /*
- генерациа и вывод бинарного числа
- int array[8];
- int power = 1;
- int decimal_number = 0;
- for (int i = 0; i < 8; i++){
- array[i] = rand()%2;
- cout << array[i];
- }*/
- // возведение числа(number) в степень(power)
- // работает до 2^64 = ~ 1.8446744*10^19
- // unsigned long long number = 9;
- // unsigned long long power = 13;
- // unsigned long long powered_num = 1;
- // for (int i = 0; i < power; i++){
- // powered_num *= number;
- // }
- // cout << powered_num;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement