I found the unsigned integer is so straight in Java. If I want to present the 0xFXXX XXXX, Java requires me to declare a long variable to store the 0xFXXX XXXX, and do a bitwise AND operation with the 4294967295(0xFFFFFFFFL).
Any one could share your experiences about the unsigned problem to others or me?
Java uses the most common method of representing signed integers - Two's complement
Might want to check this out: http://www.javamex.com/java_equivalents/unsigned.shtml
C++:
unsigned byte b = ...;
b += 100;
unsigned int v = ...;
v *= 2;
Java equivalent:
int b = ...;
b = (b + 100) & 0xff;
long v = ...;
v = (v * 2) & 0xffffffffL;
When you come to using the unsigned value, cast it to the type you want, and it will wrap naturally, giving you the correct value:
// Example: write an unsigned int (stored in a long)
// to a byte buffer.
ByteBuffer bb = ...
long unsignedInt = ...
bb.putInt((int) unsignedInt);
In java integers are 32 bits long; however, the first bit is for sign. In effect, integers are only 31 bits long.
Your number is positive 32 bit long digit; therefore, it cannot fit into integer, thus need a long.
EDIT: Why the downvote?!
Integers are represented as 2's complement, for simple 8 bit word:
MSB
0 1 1 1 1 1 1 1 = 127
0 1 1 1 1 1 1 0 = 126
0 0 0 0 0 0 1 0 = 2
0 0 0 0 0 0 0 1 = 1
0 0 0 0 0 0 0 0 = 0
1 1 1 1 1 1 1 1 = −1
1 1 1 1 1 1 1 0 = −2
1 0 0 0 0 0 0 1 = −127
1 0 0 0 0 0 0 0 = −128
MSB, the first digit effectively indicates the sign
Edit 2: I am tired of discussing a moot point. Before downvoting, refer to this
Related
Can someone explain to my why this method works, I've worked through what it does, but why does this work. Is there a pattern that binary numbers have? Like for example at i = 3, why does it do res[1] + 1 to get 2. How does res[3 >> 1] + (3&1) help to count the number of ones in the binary number of 3?
What the code should do: It works so don't worry about that. It is supposed to return a list that contains the number of ones in the binary representation of each number until num+1. And num is always >= 0. So for num = 5, you would get [0, 1, 1, 2, 1, 2], where the last index represents the number of 1s in the binary representation of 5, and the first index is number of ones in binary rep of 0.
Code:
public int[] countBits(int num) {
int[] res = new int[num+1];
for (int i = 0; i<num+1; i++){
res[i] = res[i >> 1] + (i & 1);
}
return res;
}
This is the part I can't wrap my head around:
res[i] = res[i >> 1] + (i & 1);
EDIT - This is not homework, so please fully explain your answer. This is to help with interviews.
int[] res = new int[num+1];
for (int i = 0; i<num+1; i++){
res[i] = res[i >> 1] + (i & 1);
}
return res;
rewritten as
int[] res = new int[num+1];
for (int i = 0; i<num+1; i++){
x = res[i >> 1];
y = (i & 1);
res[i] = x + y;
}
return res;
Create an array to fit the answers, +1?
for each, starting at the low end.
res[0] = res[0] + 0&1 = 0 + 0 = 0;
res[1] = res[0] + 1&1 = 0 + 1 = 1;
res[2] = res[1] + 0&1 = 1 + 0 = 0;
res[3] = res[1] + 1&1 = 1 + 1 = 2;
Looking at this pattern, I can see that because of the right shift, and the masking with &, it's splitting the problem into 2, one that's been solved previously due to the iteration order, and a bit check of the last digit.
assuming a 8 bit int, for brevity,
1 = 00000001
2 = 00000010
3 = 00000011
Split the binary into parts.
i i>>1 y&1
1 = 0000000 1
2 = 0000001 0
3 = 0000001 1
So it fetches the results for the number of ones in the first half of the array, then counts the last digit.
Because of the iteration order, and array initialisation values, this is guaranteed to work.
For values < 0 , due to 2's compliment it gets hairy, which is why it only works for values >=0
in 'res[i] = res[i >> 1] + (i & 1);'
one number's result is divide into 2 parts
the last bit is 1 or not,which can be calculate by (i & 1).
the first (n-1) bits,this number is equals to res[i >> 1]'s bitcount.this is a simple recursive call
shift by 1 gives the floor number divided by 2.
AND 1 returns 1 if the last bit of the number is 1
Hope the below table helps to see what is happening :) Just my 2 cents.
<pre>
--------------------------------
<b>
# 8 4 2 1 >>1 &1 Ans
</b>
-------------------------------
0 0 0 0 0 0 0 0
1 0 0 0 1 0 1 1
2 0 0 1 0 1 0 1
3 0 0 1 1 1 1 2
4 0 1 0 0 2 0 1
5 0 1 0 1 2 1 2
6 0 1 1 0 3 0 2
7 0 1 1 1 3 1 3
8 1 0 0 0 4 0 1
9 1 0 0 1 4 1 2
10 1 0 1 0 5 0 2
11 1 0 1 1 5 1 3
12 1 1 0 0 6 0 2
13 1 1 0 1 6 1 3
14 1 1 1 0 7 0 3
15 1 1 1 1 7 1 4
</pre>
public class UnaryOperator {
public static void main(String[] args) {
byte a= -5;
System.out.println(~a); // prints 4
}
}
When I do it manually, I get the answer as 6.
Here is how I did it:
128 64 32 16 8 4 2 1
0 0 0 0 0 1 0 1
As it is a negation I inverted it to the following:
128 64 32 16 8 4 2 1
0 0 0 0 0 1 0 1
sign -1 1 1 1 1 0 1 0
-----------------------------
0 0 0 0 1 0 1
add one--> 0 0 0 0 0 1 1
------------------------------
0 0 0 0 1 1 0 = 6
------------------------------
I know there's something wrong with what I am doing but I am not able to figure it out.
5 is 00000101
-5 is 11111010+00000001 = 11111011
~(-5) is 00000100
so you get 4.
You're starting out with -5, which is in two's complement. Thus:
-128 64 32 16 8 4 2 1
1 1 1 1 1 0 1 1 (= -5)
flip: 0 0 0 0 0 1 0 0 (= +4)
I haven't done much bitwise stuff, but after reading wikipedia for a few seconds it seems like NOT -5 = 4, on wikipedia they used NOT x = -x - 1. So the program is correct.
Edit: For unsigned integers, you use NOT x = y - x were y is the maximum number that integer can hold.
Why HashMap insert new Node on the index:
tab[(n - 1) & hash]
Where hash = key.hashCode() ^ key.hashCode() >>> 16
And n = tab.length of array of Node<K,V>.
Why HashMap not put the Node just like that: tab[hash] ? Is it just another hashing function, like multiplication by 31 in most of hashCode() methods ?
Thanks in advance for explanation!
A good description by harold but I feel it is inadequate without an example. So heres one -
Whenever a new Hasmap is created the array size of internal Node[] table is always power of 2 and following method guarantees it -
static final int tableSizeFor(int cap) {
int n = cap - 1;
n |= n >>> 1;
n |= n >>> 2;
n |= n >>> 4;
n |= n >>> 8;
n |= n >>> 16;
return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
}
So lets say you provide initial capacity as 5
cap = 5
n = cap - 1 = 4 = 0 1 0 0
n |= n >>> 1; 0 1 0 0 | 0 0 1 0 = 0 1 1 0 = 6
n |= n >>> 2; 0 0 1 1 | 0 1 1 0 = 0 1 1 1 = 7
n |= n >>> 4; 0 0 0 0 | 0 1 1 1 = 0 1 1 1 = 7
n |= n >>> 8; 0 0 0 0 | 0 1 1 1 = 0 1 1 1 = 7
n |= n >>> 16; 0 0 0 0 | 0 1 1 1 = 0 1 1 1 = 7
return n + 1 7 + 1 = 8
So table size is 8 = 2^3
Now possible index values you can put your element in map are 0-7 since table size is 8. Now lets look at put method. It looks for bucket index as follows -
Node<K,V> p = tab[i = (n - 1) & hash];
where n is the array size. So n = 8. It is same as saying
Node<K,V> p = tab[i = hash % n];
So all we need to see now is how
hash % n == (n - 1) & hash
Lets again take an example. Lets say hash of a value is 10.
hash = 10
hash % n = 10 % 8 = 2
(n - 1) & hash = 7 & 10 = 0 1 1 1 & 1 0 1 0 = 0 0 1 0 = 2
Hope this helps. More details
PS: Above link goes to my blog that has a more details example explanation on this.
Because hash may be out of range.
The "canonical solution" is to take the (positive) modulo of the hash with the length of the array, this code uses the fact that the array has a power-of-two length to replace an expensive modulo by a variable (modulo a constant is optimized pretty well) with a cheap bitwise AND.
What would (0 + 1) % 10? return? How do you deal with numbers on the left being smaller than numbers on the right? How is this even possible?
The modulo is the remainder of an integer division. Say you have integers a and b.
n = a / b (integer), and
m = a % b = a - ( b * n )
Then
b * n + m = a
Examples:
a b n = a/b b * n m = a%b
0 5 0 0 0
1 5 0 0 1
2 5 0 0 2
3 5 0 0 3
4 5 0 0 4
5 5 1 5 0
6 5 1 5 1
....
10 5 2 10 0
12 5 2 10 2
etc.
Basically, the integer division determines how many times b fully fits inside a. If b < a, that's zero times. The modulo operation then returns what is left. If b < a, that's a.
(0 + 1) % 10 would return 1
What is making you confused about small and large numbers in an expression?
As im currently aware the following is correct:
char 8 bit value e.g 0 0 0 0 0 0 0 0
short 16 bit value e.g 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
int 32 bit value e.g 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
I know the above sounds idiot proof but i want to desribe every step.
So i have the values 1 and 29 which are both 8 bits if im correct.
1: 0 0 0 0 0 0 0 1
29: 0 0 0 1 1 0 0 1
Now as these are 8 bits i can do the following
char ff = (char) 1;
char off = (char) 29;
So thats me storying my two values.
I now want to concation these values so it looks like
1 2 9 in binary that would be: 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 1
Im currently doing:
short concat = (short) (ff | off)
But get the result 29 when it should be 285 as the binary would be
32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1
0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 1
Where the hell im going wrong :(?
-- UPDATE CODE SOLUTION --
byte of= (byte) 29;
byte fm1 = (byte) 1;
char ph1 = (char) (fm1<<8 | of);
or is
short ph2 = (short) (fm1 <<8 | of);
Whats better as there both 16 bit?
System.out.println((int)ph1);
You need to shift the bits left, by 8.
short concat = (short) (ff <<8 | off)
The pipe is the bitwise or, so you just end up putting the same bits on the same place, putting 1s on places were either the first, or the second char has a 1.
char is two bytes
byte is one byte
byte ff = (byte) 1;
byte off = (byte) 29;
short concat = (ff << 8)|off;