FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm-interview/5-algorithms/ch19/72-1.py at master · eddy0604/algorithm-interview · GitHub
eddy0604
/
algorithm-interview
Public
forked from
onlybooks/python-algorithm-interview
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm-interview
/
5-algorithms
/
ch19
/
72-1.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
33 lines (27 loc) · 818 Bytes
Breadcrumbs
algorithm-interview
/
5-algorithms
/
ch19
/
72-1.py
Copy path
File metadata and controls
33 lines (27 loc) · 818 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
class
Solution
:
def
getSum
(
self
,
a
:
int
,
b
:
int
)
->
int
:
MASK
=
0xFFFFFFFF
INT_MAX
=
0x7FFFFFFF
a_bin
=
bin
(
a
&
MASK
)[
2
:].
zfill
(
32
)
b_bin
=
bin
(
b
&
MASK
)[
2
:].
zfill
(
32
)
result
=
[]
carry
=
0
sum
=
0
for
i
in
range
(
32
):
A
=
int
(
a_bin
[
31
-
i
])
B
=
int
(
b_bin
[
31
-
i
])
# 전가산기 구현
Q1
=
A
&
B
Q2
=
A
^
B
Q3
=
Q2
&
carry
sum
=
carry
^
Q2
carry
=
Q1
|
Q3
result
.
append
(
str
(
sum
))
if
carry
==
1
:
result
.
append
(
'1'
)
# 초과 자릿수 처리
result
=
int
(
''
.
join
(
result
[::
-
1
]),
2
)
&
MASK
# 음수 처리
if
result
>
INT_MAX
:
result
=
~
(
result
^
MASK
)
return
result
Back
|
FazBrowse Home
|
New Git URL