Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 940d639

Browse filesBrowse files
authored
Add files via upload
1 parent f99a262 commit 940d639
Copy full SHA for 940d639

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner
Expand file treeCollapse file tree

45 files changed

+1564
-0
lines changed
Open diff view settings
Collapse file
+45Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package Bit_Manipulation;
2+
class BitOperations{
3+
4+
public static void main(String[] args){
5+
6+
int a=15; //0000 1111
7+
int b=21; //0001 0101
8+
9+
//AND Operation
10+
/*
11+
0000 1111
12+
& 0001 0101
13+
--------------
14+
0000 0101
15+
*/
16+
int f=(a&(a-1));
17+
System.out.println("Power of 2 Operation : "+f+" Binary value: "+Integer.toBinaryString(f));
18+
19+
int and=a&b;
20+
System.out.println("AND Operation : "+and+" Binary value: "+Integer.toBinaryString(and));
21+
//OR Operation
22+
/*
23+
0000 1111
24+
| 0001 0101
25+
--------------
26+
0001 1111
27+
*/
28+
int or=a|b;
29+
System.out.println("OR Operation : "+or+" Binary value: "+Integer.toBinaryString(or));
30+
//XOR Operation
31+
/*
32+
0000 1111
33+
& 0001 0101
34+
--------------
35+
0001 1010
36+
*/
37+
int xor=a^b;
38+
System.out.println("XOR Operation : "+xor+" Binary value: "+Integer.toBinaryString(xor));
39+
int x=7;
40+
//NOT Operation
41+
int not=~x;
42+
System.out.println("NOT Operation : "+not+" Binary value: "+Integer.toBinaryString(not));
43+
44+
}
45+
}
Collapse file

‎Bit_Manipulation/BitShift.java‎

Copy file name to clipboard
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package Bit_Manipulation;
2+
3+
import java.util.Scanner;
4+
5+
class BitShift{
6+
7+
public static void main(String[] args){
8+
9+
Scanner sc=new Scanner(System.in);
10+
System.out.print("Enter a decimal number: ");
11+
int x=sc.nextInt();
12+
int a=15; //0000 1111
13+
int b=22; //0001 0101
14+
int c=24;
15+
System.out.println("Your numbers "+x+" binary value: "+Integer.toBinaryString(x));
16+
System.out.println(a+" Binary value: "+Integer.toBinaryString(a));
17+
System.out.println(b+" Binary value: "+Integer.toBinaryString(b));
18+
System.out.println(c+" Binary value: "+Integer.toBinaryString(c));
19+
System.out.println("------------------------------------------");
20+
int lshift=a<<1;
21+
System.out.println("left shift Operation : "+lshift+"\t\t Binary value: "+Integer.toBinaryString(lshift));
22+
23+
int rshift=b>>1;
24+
System.out.println("Right shift Operation : "+rshift+"\t\t Binary value: "+Integer.toBinaryString(rshift));
25+
26+
int zshift=c>>>1;
27+
System.out.println("Right shift zero fill Operation : "+zshift+"\t Binary value: "+Integer.toBinaryString(zshift));
28+
29+
System.out.println("------------------------------------------");
30+
int l=x<<1;
31+
System.out.println("left shift Operation on your number: "+l+"\t\t Binary value: "+Integer.toBinaryString(l));
32+
int r=x>>1;
33+
System.out.println("Right shift Operation on your number: "+r+"\t\t Binary value: "+Integer.toBinaryString(r));
34+
int rz=x>>>1;
35+
System.out.println("Right shift zero fill Operation on your number: "+rz+"\t\t Binary value: "+Integer.toBinaryString(rz));
36+
System.out.println("-------------------------------------------");
37+
38+
sc.close();
39+
}
40+
}
Collapse file
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package Bit_Manipulation;
2+
3+
import java.util.Scanner;
4+
5+
public class PowerOfNumber {
6+
7+
public static void main(String[] args){
8+
Scanner sc=new Scanner(System.in);
9+
// System.out.print("Enter the Number: ");
10+
// int n=sc.nextInt();
11+
System.out.print("Upto: ");
12+
int x=sc.nextInt();
13+
for(int i=0;i<=x;i++){
14+
int t=2<<i;
15+
System.out.print(t+" ");
16+
}
17+
sc.close();
18+
}
19+
}
Collapse file
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package Bit_Manipulation;
2+
3+
import java.util.Scanner;
4+
5+
public class checPositiveNum {
6+
7+
public static void main(String[] args) {
8+
9+
Scanner sc = new Scanner(System.in);
10+
System.out.print("Enter the Number: ");
11+
int n = sc.nextInt();
12+
if (n >> 31 == 0)
13+
System.out.println("Positive Number!");
14+
else
15+
System.out.println("Negative Number!");
16+
sc.close();
17+
}
18+
}
Collapse file

‎Bit_Manipulation/checkBitSet.java‎

Copy file name to clipboard
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Bit_Manipulation;
2+
3+
import java.util.Scanner;
4+
5+
public class checkBitSet {
6+
7+
public static void main(String... args){
8+
Scanner sc=new Scanner(System.in);
9+
System.out.print("Enter the Number: ");
10+
int num=sc.nextInt();
11+
System.out.print("Position to check: ");
12+
int pos=sc.nextInt();
13+
System.out.println("Your number is: "+Integer.toBinaryString(num));
14+
if(!checkBit(num,pos)){
15+
System.out.println("The "+pos+" bit is set(or 1). ");
16+
}else{
17+
System.out.println("The "+pos+" bit is not set(or 0).");
18+
}
19+
sc.close();
20+
}
21+
static boolean checkBit(int n,int p){
22+
boolean res=((n&(1<<p))==0)?true:false;
23+
return res;
24+
}
25+
26+
}
Collapse file
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package Bit_Manipulation;
2+
3+
import java.util.Scanner;
4+
5+
public class countNumOfOne {
6+
public static void main(String... args){
7+
Scanner sc=new Scanner(System.in);
8+
System.out.println("Enter the Number: ");
9+
int num=sc.nextInt();
10+
System.out.print("The Number of one's in : "+num+" ("+Integer.toBinaryString(num)+")");
11+
int result=countOne(num);
12+
System.out.println(" is : "+result);
13+
sc.close();
14+
}
15+
static int countOne(int n){
16+
int count=0;
17+
while(n>0){
18+
n=n&(n-1);
19+
count++;
20+
}
21+
return count;
22+
}
23+
}
Collapse file

‎Bit_Manipulation/lowerToUpper.java‎

Copy file name to clipboard
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package Bit_Manipulation;
2+
3+
import java.util.Scanner;
4+
5+
public class lowerToUpper {
6+
7+
public static void main(String[] args){
8+
Scanner sc=new Scanner(System.in);
9+
System.out.print("Enter the lowercase Character: ");
10+
char ch=sc.next(".").charAt(0); //it will take only one character(Strict)
11+
// char c=sc.findInLine(".").charAt(0); //it will take first line from the characters
12+
int n=(int)ch;
13+
n=(n&'_');
14+
System.out.println("Your char '"+ch+"' in UpperCase : "+(char)n);
15+
16+
sc.close();
17+
18+
}
19+
}
Collapse file

‎Bit_Manipulation/oddEven.java‎

Copy file name to clipboard
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package Bit_Manipulation;
2+
3+
import java.util.Scanner;
4+
5+
public class oddEven {
6+
public static void main(String[] args){
7+
Scanner sc=new Scanner(System.in);
8+
System.out.print("Enter a number to check: ");
9+
int x=sc.nextInt();
10+
if(checkOddEven(x)){
11+
System.out.println(x+" is a Even Number. ");
12+
}else{
13+
System.out.println(x+" is a Odd Number. ");
14+
}
15+
sc.close();
16+
}
17+
static boolean checkOddEven(int num){
18+
return ((num & 1)==0);
19+
}
20+
21+
}
Collapse file

‎Bit_Manipulation/powerOfTwo.java‎

Copy file name to clipboard
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package Bit_Manipulation;
2+
3+
import java.util.Scanner;
4+
5+
public class powerOfTwo {
6+
public static void main(String[] args){
7+
Scanner sc=new Scanner(System.in);
8+
System.out.print("Enter a number to check: ");
9+
int x=sc.nextInt();
10+
11+
if(isPowerOfTwo(x)){
12+
System.out.println("Power of two!");
13+
14+
}else{
15+
System.out.println("Not a power of two!!");
16+
}
17+
sc.close();
18+
}
19+
static boolean isPowerOfTwo(int n){
20+
return (n>0)&&((n&(n-1))==0);
21+
}
22+
23+
24+
}
Collapse file

‎Bit_Manipulation/subsetOfSet.java‎

Copy file name to clipboard
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package Bit_Manipulation;
2+
3+
import java.util.Scanner;
4+
5+
/*
6+
* Generate possible Subsets for given set.
7+
*
8+
* */
9+
10+
public class subsetOfSet {
11+
public static void main(String... args) {
12+
Scanner sc = new Scanner(System.in);
13+
// char ch[]={'a','b','c','d'};
14+
System.out.print("How many element in set? ");
15+
int n = sc.nextInt();
16+
char[] ch = new char[n];
17+
System.out.print("Enter the char elements: ");
18+
for (int k = 0; k < n; k++) {
19+
char c = sc.next().charAt(0);
20+
ch[k] = c;
21+
}
22+
subsetOfSet.possibleSubset(ch, n);
23+
sc.close();
24+
}
25+
26+
static void possibleSubset(char[] c, int n) {
27+
28+
for (int i = 0; i < (1 << n); ++i) {// here i loop until it reaches
29+
// 2powN (1<<n). bcz
30+
// there are 2powN subset for a set
31+
// with N element.
32+
System.out.print("{");
33+
for (int j = 0; j < n; j++) {
34+
if ((i & (1 << j)) != 0) {
35+
System.out.print(c[j] + " ");
36+
}
37+
}
38+
System.out.print("}, ");
39+
}
40+
}
41+
42+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.