-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitmanipulation.java
126 lines (92 loc) · 2.88 KB
/
bitmanipulation.java
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.util.*;
public class bitmanipulation {
public static void main(String []args){
//getbit(5,2);
// setbit(5,1);
//
//clearbit(5,2);
// System.out.println(ispoweroftwo(4));
//
// toggle(3,2);
// System.out.println(countone(5));
// tobinary(150);
toDecimal("1010");
}
static void setbit(int n, int pos){
int bitmask=1<<pos;
System.out.println(bitmask|n);
}
static void getbit(int n,int pos){
int bitmask=1<<pos;
if((n&bitmask)==0){
System.out.println("Bit is zero");
}else{
System.out.println("Bit is One");
}
}
static void clearbit(int n,int pos){
int bitmask = 1<<pos;
int notbitmask = ~(bitmask);
System.out.println(n¬bitmask);
}
//a program to find if a number is a power of 2 or not.
static boolean ispoweroftwo(int n){
if(n<=0){
return false;
}
return (n&(n-1))==0;
}
//a program to toggle a bit a position = “pos” in a number “n”.
static void toggle(int n,int pos){
int bitmask=1<<pos;
int result =n^bitmask;
System.out.println(result);
}
//a program to count the number of 1’s in a binary representation of the number.
public static int countone(int n){
int counts=0;
while(n>0){
int remainder=n%2;
if(remainder==1){
counts++;
}
n=n/2;
}
return counts;
}
//Write 2 functions => decimalToBinary() & binaryToDecimal() to convert a number from one number system to another
public static void tobinary(int n){
StringBuffer sf= new StringBuffer(100);
while(n>0){
int remainder=n%2;
if(remainder==1){
sf.append('1');
}else{
sf.append('0');
}
n=n/2;
}
String binary=sf.toString();
for(int i=(binary.length()-1);i>=0;i--){
System.out.print(binary.charAt(i));
}
}
public static void toDecimal(String Binary){
// Initialize index 'i' to the last position of the binary string
int i = Binary.length() - 1;
// Initialize a variable 'num' to store the decimal value
double num = 0;
// Loop through the binary string from right to left
while(i >= 0){
// Check if the current bit is '1'
if(Binary.charAt(i) == '1'){
// If true, add the corresponding decimal value (2^position) to 'num'
num += Math.pow(2, Binary.length() - i - 1);
}
// Move to the next bit (leftwards)
i--;
}
// Print the final decimal value
System.out.println(num);
}
}