bitwise-operators

How do I set multiple input types in an EditText on Android?

北慕城南 提交于 2020-03-17 08:35:05
问题 I am trying to create an EditText with auto-capitalization and auto-correction implemented. I have manually figured out how to add InputFilter s to allow auto-capitalization, though this only works after the first letter is typed, and I have had no luck with auto correction (I tried to create an InputFilter that used AutoText , but I'm not sure how all that works). Ideally, I could just use EditText.setInputType(...) to handle everything, but so far this has not worked. Is there a way to

How does a bitwise AND (or TEST) with 16 test the 5th bit?

ぐ巨炮叔叔 提交于 2020-02-25 06:08:59
问题 In my College's documentation on 8086 Assembly is the following example: TEST AL, 16 ; tests the 5th bit's state Is this at all correct given what the TEST instruction does? It sets flags based on AL & 16 . How does that test the 5th bit? NOTE: there's no previously mentioned value to AL, just exactly what's shown here, so I assume this has to work in the general case. 回答1: 16 in decimal is 10000 in binary. Notice the fifth bit from the right is set, and that it is the only one. TEST is

what is the means of i &=(i-1) in java

一笑奈何 提交于 2020-02-08 02:58:52
问题 int n; for ( n = 0; i >0; n++) { i &= (i-1); } return n; //maybe its function is to count the number of 1, I don't know the sentence means 回答1: This is indeed counting the number of one-bits in the value of i. It does however only work properly for positive values. The JRE provided Integer.bitCount(i) does the same and it works in constant time as well as for negative values. As for how it works, its hard to understand if you're not familar with binary arithmetic. What basically happens that

Difference between bitwise inclusive or and exclusive or in java

半世苍凉 提交于 2020-01-31 05:12:09
问题 public class Operators { public static void main(String[] args) { int a = 12; System.out.println("Bitwise AND:"+(12&12)); System.out.println("Bitwise inclusive OR:"+(12|12)); System.out.println("Bitwise exclusive OR:"+(12^12)); } } OUTPUT: Bitwise AND:12 Bitwise inclusive OR:12 Bitwise exclusive OR:0 I understand first two, but not the third. 回答1: XOR tells whether each bit is different. 1 XOR 1 = 0 1 XOR 0 = 1 0 XOR 1 = 1 0 XOR 0 = 0 In other words "either but not both" 0011 XOR 0101 = 0110

Writing a stream of 9 bit values as bytes to a file in C

无人久伴 提交于 2020-01-30 09:05:20
问题 I have an array with integer values from 0-511 (9 bits max). I am trying to write this to a file with fwrite . For Example, with the array: [257, 258, 259] Which is 100000001, 100000010, 100000011 I am trying to write 100000001100000010100000011 + extra padding of 0s to the file But since fwrite limits writes to 1 byte at a time, I am not sure how to go about doing this. I am new to bitwise operations and am not how to separate out the individual bytes. 回答1: You need a bit buffer. Since you

Bit-operation OR vs addition

偶尔善良 提交于 2020-01-25 20:56:37
问题 I am reading an uint16 from a sensor connected to an raspberry (arm). I convert the data from little endian to big endian via: // result = 0A 0B // 0B 00 | 00 0A (result << 8) | (result >> 8); So 0A 0B is 0B 0A afterwards. But I also saw people using this: (result << 8) + (result >> 8); Is there any advantage of using the addition? My guess is, there is no really advantage, it is just a bit slower. There is a big difference when it comes to sum two numbers for example: EF10 = 0FF0 + 00FF !=

Logical vs. bitwise operator AND

给你一囗甜甜゛ 提交于 2020-01-24 09:40:10
问题 I don’t understand the difference between & and and , even if I read some other questions about it. My code is: f=1 x=1 f==1 & x==1 Out[60]: True f==1 and x==1 Out[61]: True f=1 x=2 f==1 and x==2 Out[64]: True f==1 & x==2 Out[65]: False Why is it the second & False , whereas the first is True ? 回答1: The issue is that & has higher operator precedence than == . >>> (f == 1) & (x == 2) True >>> f == (1 & x) == 2 False Perhaps this seems unintuitive, but & is really meant to be used between

How to make a hashcode(integer value) positive

谁都会走 提交于 2020-01-24 03:47:11
问题 int x = 10; int y = (x.hashcode() & 0xfffffff); How does the above code always make y positive ? Thanks! 回答1: x.hashcode() & 0xfffffff will turn the sign bit off. Math.abs is not used here because it returns negative if x.hashCode is equal to Integer.MIN_VALUE which will make the hashtable's array throw an ArrayOutOfBoundException which is not fun. From @JonSkeet comment: It doesn't just turn the sign bit off, it clears the next three bits as well . But with hash codes we deal with collisions

Bitwise left shift behaviour

旧巷老猫 提交于 2020-01-22 23:04:31
问题 Today I was learning about the left shift bit operator ( << ). As I understand it the left shift bit operator moves bits to the left as specified. And also I know multiply by 2 for shifting. But I am confused, like what exactly is the meaning of "shifting bits" and why does the output differ when value is assigned with a different type? When I call the function below, it gives output as System.out.println("b="+b); //Output: 0 And my question is: how does b become 0 and why is b typecasted?

Bitwise left shift behaviour

爷,独闯天下 提交于 2020-01-22 23:03:50
问题 Today I was learning about the left shift bit operator ( << ). As I understand it the left shift bit operator moves bits to the left as specified. And also I know multiply by 2 for shifting. But I am confused, like what exactly is the meaning of "shifting bits" and why does the output differ when value is assigned with a different type? When I call the function below, it gives output as System.out.println("b="+b); //Output: 0 And my question is: how does b become 0 and why is b typecasted?