DecimalToBinary() function that will take Decimal number as int, and return its Binary equivalent as a string.
(num int)
| 32 | // DecimalToBinary() function that will take Decimal number as int, |
| 33 | // and return its Binary equivalent as a string. |
| 34 | func DecimalToBinary(num int) (string, error) { |
| 35 | if num < 0 { |
| 36 | return "", errors.New("integer must have +ve value") |
| 37 | } |
| 38 | if num == 0 { |
| 39 | return "0", nil |
| 40 | } |
| 41 | var result string = "" |
| 42 | for num > 0 { |
| 43 | result += strconv.Itoa(num & 1) |
| 44 | num >>= 1 |
| 45 | } |
| 46 | return Reverse(result), nil |
| 47 | } |