I'm working on a program for class and was asked to figure out what <<= means and I found that << means a left shift of bits. I'm not sure what a left shift of bits is either though
This is equivalent to += or -= or similar operators. The operator << makes a copy of the variable and shifts it left. You must then assign this to a variable or use it in some way. The code:
x << 2;
does nothing. You must use this value in some way:
x = x << 2;
x <<= 2;
These are equivalent statements.
A left shift operator simply moves every bit of a variable to the left. Say we have defined the following variables:
char c = 5;
char d = 5;
lets say char is a 8-bit variable. In this case, c and d's binary form would look like this:
00000101
When a left shift is performed on c, every bit would move to the left:
c <<= 1; // now c = 00001010
d <<= 2; // now d = 00010100
Related
if I have this:
var5 = (this.unknownInt1900 >> 4) - 1;
and I know the value of var5
how do I solve for unknownInt1900?
E.g. unknownInt1900 = var5 + 1 (and I don't know what do with the bitshift here)
It's not reversible if you had data in the lower 4 bits. If you know that those bits are empty, just add and left shift:
unknownInt1900 = (var5 + 1) << 4;
public class Operator {
public static void main(String[] args) {
byte a = 5;
int b = 10;
int c = a >> 2 + b >> 2;
System.out.print(c); //prints 0
}
}
when 5 right shifted with 2 bits is 1 and 10 right shifted with 2 bits is 2 then adding the values will be 3 right? How come it prints 0 I am not able to understand even with debugging.
This table provided in JavaDocs will help you understand Operator Precedence in Java
additive + - /\ High Precedence
||
shift << >> >>> || Lower Precedence
So your expression will be
a >> 2 + b >> 2;
a >> 12 >> 2; // hence 0
It's all about operator precedence. Addition operator has more precedence over shift operators.
Your expression is same as:
int c = a >> (2 + b) >> 2;
Is this what you want?
int c = ((a >> 2) + b) >> 2;
You were shifting to the right by whatever is 2+b. I assume you wanted to shift 5 by 2 positions, right?
b000101 >> 2 == b0001ΓΈΓΈ
| |___________________|_|
| |
|_____________________|
i.e. the leftmost bit shifts to the right by 2 positions and right most bit does as well (but is has no more valid positions left on its right side so it simply disappears) and the number becomes what's left - in this case '1'. If you shift number 5 by 12 positions you will get zero as 5 has less than 12 positions in binary form. In case of '5' you can shift by 2 positions at most if you want to preserve non-zero value.
Consider:
int a = 0;
a |= 1 << a;
System.out.println(a);
It prints "1". Why? I thought left bit shifting 0 by any number of times was still 0. Where's it pulling the 1 from?
The expression 1 << a; will shift the value 1, a number of times.
In other words, you have the value 1:
0000001
Now, you shift the whole thing over 0 bits to the left. You then have:
0000001
You then have:
a |= 1 << a;
Which resolves to:
a = 0000000 | 0000001
Or:
a = 1;
You might have the operands mixed up. If you're trying to shift the value 0 one bit to the left, you'd want:
a |= a << 1;
You are using the operator << in a wrong way.
It must to be:
int a = 0;
a |= a << 1;
System.out.println(a);
You are left shifting the literal 1 by the variable a. The value of variable a is zero. 1<<0 = 1
So you've just got your variables flipped. Try reversing the variables.
The following code sample prints 1.5.
float a = 3;
float b = 2;
a /= b;
System.out.println(a);
I don't understand what the /= operator does. What is it supposed to represent?
It's a combination division-plus-assignment operator.
a /= b;
means divide a by b and put the result in a.
There are similar operators for addition, subtraction, and multiplication: +=, -= and *=.
%= will do modulus.
>>= and <<= will do bit shifting.
It is an abbreviation for x = x / y (x /= y). What it does is it divides the variable to be asigned by the left hand side of it and stores it in the right hand side. You can always change:
x = x / y
to
x /= y
You can do this with most other operators like * / + and -. I am not sure about bitwise operators though.
a/=b; implies that divide a with b and put the result into a
A/=B means the same thing as A=(A/B)
Java (copying from C) has a whole set of operators X op = Y meaning X=X op Y, for op being any of: + - * / % & | ^
X/=Y it is same as X=X/Y.
Also you can try the same thing for these operators + - * %
As most people here have already said, x/=y is used as a shortened form of x=x\y and the same works for other operators like +=, -=, *=, %=. There's even further shortcuts for + and - where x++ and x-- represent x=x+1 and x=x-1 respectively.
One thing that hasn't been mentioned is that if this is used in some function call or conditional test the original value is used then replaced, letting you do the iterator for a while loop inside the call that uses that iterator, or write a true mod function using the remainder operator much more concisely:
public int mod(int a, int b){
return ((a%=b)>=0?a:a+b);
}
which is a much shorter way of writing:
public int mod(int a, int b){
a = a % b
if (a>=0){
return a;
}
return a+b;
}
You could say a = a / b OR (for short) say a /= b
Here is the problem:
You're given 2 32-bit numbers, N & M, and two bit positions, i & j. write a method to set all bits between i and j in N equal to M (e.g. M becomes a substring of N at locating at i
and starting at j)
For example:
input:
int N = 10000000000, M = 10101, i = 2, j = 6;
output:
int N = 10001010100
My solution:
step 1: compose one mask to clear sets from i to j in N
mask= ( ( ( ((1<<(31-j))-1) << (j-i+1) ) + 1 ) << i ) - 1
for the example, we have
mask= 11...10000011
step 2:
(N & mask) | (M<<i)
Question:
what is the convenient data type to implement the algorithm? for example
we have int n = 0x100000 in C, so that we can apply bitwise operators on n.
in Java, we have BitSet class, it has clear, set method, but doesnt support
left/right shift operator; if we use int, it supports left/right shift, but
doesnt have binary representation (I am not talking about binary string representation)
what is the best way to implement this?
Code in java (after reading all comments):
int x = Integer.parseInt("10000000000",2);
int x = Integer.parseInt("10101",2);
int i = 2, j = 6;
public static int F(int x, int y, int i, int j){
int mask = (-1<<(j+1)) | (-1>>>(32-i));
return (mask & x ) | (y<<i);
}
the bit-wise operators |, &, ^ and ~ and the hex literal (0x1010) are all available in java
32 bit numbers are ints if that constraint remains int will be a valid data type
btw
mask = (-1<<j)|(-1>>>(32-i));
is a slightly clearer construction of the mask
Java's int has all the operations you need. I did not totally understand your question (too tired now), so I'll not give you a complete answer, just some hints. (I'll revise it later, if needed.)
Here are j ones in a row: (1 << j)-1.
Here are j ones in a row, followed by i zeros: ((1 << j) - 1) << i.
Here is a bitmask which masks out j positions in the middle of x: x & ~(((1 << j) - 1) << i).
Try these with Integer.toBinaryString() to see the results. (They might also give strange results for negative or too big values.)
I think you're misunderstanding how Java works. All values are represented as 'a series of bits' under the hood, ints and longs are included in that.
Based on your question, a rough solution is:
public static int applyBits(int N, int M, int i, int j) {
M = M << i; // Will truncate left-most bits if too big
// Assuming j > i
for(int loopVar = i; loopVar < j; loopVar++) {
int bitToApply = 1 << loopVar;
// Set the bit in N to 0
N = N & ~bitToApply;
// Apply the bit if M has it set.
N = (M & bitToApply) | N;
}
return N;
}
My assumptions are:
i is the right-most (least-significant) bit that is being set in N.
M's right-most bit maps to N's ith bit from the right.
That premature optimization is the root of all evil - this is O(j-i). If you used a complicated mask like you did in the question you can do it in O(1), but it won't be as readable, and readable code is 97% of the time more important than efficient code.