FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Algorithm/AddTwoBitStrings.cpp at main · vatsalya1102/Algorithm · GitHub
vatsalya1102
/
Algorithm
Public
forked from
heyimShivam/Algorithm
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
/
AddTwoBitStrings.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
58 lines (44 loc) · 1005 Bytes
Breadcrumbs
Algorithm
/
AddTwoBitStrings.cpp
Copy path
File metadata and controls
58 lines (44 loc) · 1005 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#
include
<
iostream
>
using
namespace
std
;
string
addBitStrings
( string first, string second );
int
makeEqualLength
(string &str1, string &str2)
{
int
len1 = str1.
size
();
int
len2 = str2.
size
();
if
(len1 < len2)
{
for
(
int
i =
0
; i < len2 - len1 ; i++)
str1 =
'
0
'
+ str1;
return
len2;
}
else
if
(len1 > len2)
{
for
(
int
i =
0
; i < len1 - len2 ; i++)
str2 =
'
0
'
+ str2;
}
return
len1;
}
string
addBitStrings
( string first, string second )
{
string result;
int
length =
makeEqualLength
(first, second);
int
carry =
0
;
for
(
int
i = length-
1
; i >=
0
; i--)
{
int
firstBit = first.
at
(i) -
'
0
'
;
int
secondBit = second.
at
(i) -
'
0
'
;
int
sum = (firstBit ^ secondBit ^ carry)+
'
0
'
;
result = (
char
)sum + result;
carry = (firstBit & secondBit) | (secondBit & carry) | (firstBit & carry);
}
if
(carry)
result =
'
1
'
+ result;
return
result;
}
int
main
()
{
string str1 =
"
1100011
"
;
string str2 =
"
10
"
;
cout <<
"
Sum is
"
<<
addBitStrings
(str1, str2);
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL