FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/Conversions/DecimalToBinary.java at master · java66liu/Java · GitHub
java66liu
/
Java
Public
forked from
TheAlgorithms/Java
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
Java
/
Conversions
/
DecimalToBinary.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
60 lines (54 loc) · 1.49 KB
Breadcrumbs
Java
/
Conversions
/
DecimalToBinary.java
Copy path
File metadata and controls
60 lines (54 loc) · 1.49 KB
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
59
60
package
Conversions
;
import
java
.
util
.
Scanner
;
/**
* This class converts a Decimal number to a Binary number
*
*
*/
class
DecimalToBinary
{
/**
* Main Method
*
* @param args Command Line Arguments
*/
public
static
void
main
(
String
args
[]) {
conventionalConversion
();
bitwiseConversion
();
}
/**
* This method converts a decimal number
* to a binary number using a conventional
* algorithm.
*/
public
static
void
conventionalConversion
() {
int
n
,
b
=
0
,
c
=
0
,
d
;
Scanner
input
=
new
Scanner
(
System
.
in
);
System
.
out
.
printf
(
"Conventional conversion.
\n
\t
Enter the decimal number: "
);
n
=
input
.
nextInt
();
while
(
n
!=
0
) {
d
=
n
%
2
;
b
=
b
+
d
* (
int
)
Math
.
pow
(
10
,
c
++);
n
/=
2
;
}
//converting decimal to binary
System
.
out
.
println
(
"
\t
Binary number: "
+
b
);
input
.
close
();
}
/**
* This method converts a decimal number
* to a binary number using a bitwise
* algorithm
*/
public
static
void
bitwiseConversion
() {
int
n
,
b
=
0
,
c
=
0
,
d
;
Scanner
input
=
new
Scanner
(
System
.
in
);
System
.
out
.
printf
(
"Bitwise conversion.
\n
\t
Enter the decimal number: "
);
n
=
input
.
nextInt
();
while
(
n
!=
0
) {
d
= (
n
&
1
);
b
+=
d
* (
int
)
Math
.
pow
(
10
,
c
++);
n
>>=
1
;
}
System
.
out
.
println
(
"
\t
Binary number: "
+
b
);
input
.
close
();
}
}
Back
|
FazBrowse Home
|
New Git URL