forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_neg2_conversion.py
More file actions
Latest commit
37 lines (31 loc) · 797 Bytes
/
Copy pathbase_neg2_conversion.py
File metadata and controls
37 lines (31 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
defdecimal_to_negative_base_2(num: int) ->int:
"""
This function returns the number negative base 2
of the decimal number of the input data.
Args:
int: The decimal number to convert.
Returns:
int: The negative base 2 number.
Examples:
>>> decimal_to_negative_base_2(0)
0
>>> decimal_to_negative_base_2(-19)
111101
>>> decimal_to_negative_base_2(4)
100
>>> decimal_to_negative_base_2(7)
11011
"""
ifnum==0:
return0
ans=""
whilenum!=0:
num, rem=divmod(num, -2)
ifrem<0:
rem+=2
num+=1
ans=str(rem) +ans
returnint(ans)
if__name__=="__main__":
importdoctest
doctest.testmod()