shouldn't
0b11110001>>4 = 0b11111111
OR
0bf1>>4 = 0bff?
calculator shows:
0b0f or 0b00001111
bitwise
Resolved
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
21 August 2023, 18:19solution
When you shift right using the signed operator it will fill with zeros on the left if the number is positive, or ones if the number is negative, while dropping digits from the right.
In your example 0b11110001 >> 4 would equal 0b1111, or 15.
Anything starting with 0b are binary numbers and can only have 1 or 0 as digits. This means 0bf1, 0bff, 0bf1 are invalid. I think you are meaning that these numbers should be hexadecimal, which would be written with 0x and not 0b.
0xf1 is the same as 0b11110001 which means 0xf1 >> 4 also results in 15 (0xf would be how it is written in hexadecimal)
+2
Harshit GargExpert
23 August 2023, 01:48
Thanks, but if a binary starts with 1, isnt it negative
and the one that starts with 0 , positive?
+1
Guadalupe Gagnon
23 August 2023, 02:55
no
only the left most bit is reserved as the switch for positive or negative numbers. So for any negative integer the number would start with a one and then 31 zeros and ones. If a binary number is less than 32 digits then it is not negative in Java.
this is a positive number because its less than 32 digits
1101011010110100101000101010100
this is a negative number because it is 32 digits and the left most is 1
10001011010000100101000101000000
+1