Home
Browse
Create
Search
Log in
Sign up
Upgrade to remove ads
Only $2.99/month
Bit Manipulation
STUDY
Flashcards
Learn
Write
Spell
Test
PLAY
Match
Gravity
Terms in this set (12)
printBinary(double d)
private String printBinary(double num){
if(num >= 1 || num <= 0)
return "ERROR";
StringBuilder binVal = new StringBuilder();
binVal.append(".");
while(num > 0){
// if we go over 32 characters, return ERROR
if(binVal.length() >= 32)
return "ERROR";
double rem = num * 2; // shift decimal point to the left
if(rem >= 1){
binVal.append(1);
num = rem - 1;
}else{
binVal.append("0");
num = rem;
}
}
return binVal.toString();
}
Logical Shift
In a Logical Shift Right, we shift the bits and put a 0 in the most significant bid. It is indicated with a >>> operator.
ex: 10110101 = -75
>>> 1
01011010 = 90
Arithmetic Shift
The arithmetic right shift essentially divides by 2. It is identified by the >> operator.
getBit
Method shift 1 over by i bits, creating a value like: 0001000. Then perform an AND with num, clearing all bits other than i-th bit.
boolean getBit(int num, int i){
return ((num & (1 << i)) != 0);
}
countNumberOfOnes algorithm
Iterate through each digit by doing an arithmetic shift right and count the 1 at the least significant bit by performing an AND comparison with 1.
public int countOneBits(int num){
int count=0;
while(num != 0){
count += num & 1; //
num >> 1; // shift right by 1
}
return count;
}
/*
Ex: 0101
0101
AND 0001 ==> 1
>> 1 010 010
AND 001 ==> 0
>> 1 01 01
AND 01 ==> 1
>> 1 00 00
AND 01 ==> 0
count = 1 + 0 + 1 + 0 == 2
*/
mask for just odd number bits
*odd bits are one.
binary: 0101 0101 010
hex: 0xaaaaaaaa
mask for just even number of bits
*event bits are one
binary: 1010 1010 101
hex: 0x55555555
setBit
setBit shifts 1 over by i bits, creating a value like 0001000. By performing an OR with num, only the value of bit i will change.
int setBit(int num, int i){
int mask = 1 << i;
return num | mask;
}
updateBit
to set the i-th bit to a value v, we first clear the bit at position i by using a mask that looke like 11101111. Then we shift the intended value, v, left by i bits. This will creae a number with bit i equal to v and all other bits equal to 0. Finally, we OR these two numbers, updating the i-th bit if v is 1, and 0 otherwise.
int updateBig(int num, int i, boolean bitIs1){
int value = bitIs1? 1: 0;
int mask = ~( 1 << i );
return (num & mask) | (value << i);
}
clearBit
this is almost opposite of setBit. First we create a number like 11101111 by creating the reverse of (0001000) and negating it. Then we perform an AND operation with num, which will clear the i-th bit.
int clearBit(int num, int i){
int mask = ~( 1 << i );
return num & mask;
}
clear most significant bit through i-th bit
To clear all bits from most significant bit through i, we create a mask with a 1 at the i-th bit (1 << i). Then we subtract 1 from it, giving us a sequence of 0s followed by i 1s. We then AND our number with this mask to leave just the last i bits.
int clearBitsMSBthroughI(int num, int i){
int mask = (1 << i ) - 1;
return num & mask;
}
clear all bits from i thorugh 0
to clear all bits from i through 0, we take a sequence of all 1s ( which is -1), then we want a logicl shift ( so that we move the sign bit), So we use the >>> operator. This gives us a sequence of 1s followed by i 0 bits.
int clearBitsIthrough0(int num, int i){
int mask = ~(-1 >>> (31 - i));
return num & mask;
}
THIS SET IS OFTEN IN FOLDERS WITH...
Math and Logic Puzzles
10 terms
Recursion and Dynamic Programming
12 terms
Arrays and Strings
4 terms
Java
3 terms
YOU MIGHT ALSO LIKE...
Chapter 13: Recursions
101 terms
9.19.2015 CSC108
77 terms
Java Chapter 5
26 terms
code hs methods unit test
25 terms
OTHER SETS BY THIS CREATOR
Amazon Leadership Principals
14 terms
Layer 7
24 terms
Book Outline
2 terms
Java - Collections
10 terms