본문 바로가기

bin4

1129. Bin to Decimal Complete the function which converts a binary number (given as a string) to a decimal number. Solution: int binToDec(String bin) => int.parse(bin, radix: 2); 2022. 11. 29.
Binary Addition Description: Implement a function that adds two numbers together and returns their sum in binary. The conversion can be done before, or after the addition. The binary number returned should be a string. Examples:(Input1, Input2 --> Output (explanation))) 1, 1 --> "10" (1 + 1 = 2 in decimal or 10 in binary) 5, 9 --> "1110" (5 + 9 = 14 in decimal or 1110 in binary) Solution: 1. Add 'a' and 'b'. 2... 2022. 2. 27.
Bit Counting 문제 설명 Write a function that takes an integer as input, and returns the number of bits that are equal to one in the binary representation of that number. You can guarantee that input is non-negative. Example: The binary representation of 1234 is 10011010010, so the function should return 5 in this case 해결 방법 1. 들어온 수를 2진수로 바꾼다. 2. 바꾼 수에서 '1'의 개수를 세어 반환한다. def count_bits(n): return bin(n).count('1.. 2022. 2. 16.
Ones and Zeros 문제 설명 Given an array of ones and zeroes, convert the equivalent binary value to an integer. Eg: [0, 0, 0, 1] is treated as 0001 which is the binary representation of 1. Examples: Testing: [0, 0, 0, 1] ==> 1 Testing: [0, 0, 1, 0] ==> 2 Testing: [0, 1, 0, 1] ==> 5 Testing: [1, 0, 0, 1] ==> 9 Testing: [0, 0, 1, 0] ==> 2 Testing: [0, 1, 1, 0] ==> 6 Testing: [1, 1, 1, 1] ==> 15 Testing: [1, 0, 1, 1] .. 2022. 2. 12.