bitwise-operators

Remove a Paint Flag in Android

荒凉一梦 提交于 2019-12-17 21:45:10
问题 My code looks like this: TextView task_text = (TextView) view.findViewById(R.id.task_text); task_text.setPaintFlags( task_text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); This causes a strike through effect to appear on the text. However, I'd like to know how to remove the flag once set, and how to detect that the flag is set. I understand this is a bitwise operation, but I've tried both ~ and - operators, neither work. 回答1: To remove a flag, this should work: task_text.setPaintFlags(

What does ~0 do?

喜你入骨 提交于 2019-12-17 20:59:46
问题 Does ~0 mean its flipping 000000000 to 1111111111? printf("Check: %i", ~0); The printf results to -1, which is why I am confused. Does -1 essentially mean the same thing as 11111111111111111 bits? 回答1: Does ~0 mean its flipping 000000000 to 1111111111? Yes, it does. Does -1 essentially mean the same thing as 11111111111111111 bits? In 2s complement representation, it does. 回答2: Does ~0 mean its flipping 000000000 to 1111111111? Yes, that's what it means. printf("Check: %i", ~0); The printf

Javascript & PHP Xor equivalent

巧了我就是萌 提交于 2019-12-17 16:40:12
问题 I have a javascript code: var c = 267414715; var d = c ^ ("0x81BE16CD"); Result is -1907459466 http://jsfiddle.net/4N3JY/1/ I can't seem to get a PHP equivalent. Have tried the following: <?php $c=267414715; $d=$c ^ hexdec("0x81BE16CD"); echo "With hexdec: $d\n"; $d=$c ^ base_convert("0x81BE16CD", 16, 2); echo "With base_convert(2): $d\n"; $d=$c ^ base_convert("0x81BE16CD", 16, 10); echo "With base_convert(10): $d\n"; ?> Output: With hexdec: 2387507830 With base_convert(2):

Javascript & PHP Xor equivalent

牧云@^-^@ 提交于 2019-12-17 16:39:59
问题 I have a javascript code: var c = 267414715; var d = c ^ ("0x81BE16CD"); Result is -1907459466 http://jsfiddle.net/4N3JY/1/ I can't seem to get a PHP equivalent. Have tried the following: <?php $c=267414715; $d=$c ^ hexdec("0x81BE16CD"); echo "With hexdec: $d\n"; $d=$c ^ base_convert("0x81BE16CD", 16, 2); echo "With base_convert(2): $d\n"; $d=$c ^ base_convert("0x81BE16CD", 16, 10); echo "With base_convert(10): $d\n"; ?> Output: With hexdec: 2387507830 With base_convert(2):

Is there any way to write “mod 31” without modulus/division operators?

▼魔方 西西 提交于 2019-12-17 12:34:56
问题 Getting the modulus of a number can be easily done without the modulus operator or divisions, if your operand is a power of 2. In that case, the following formula holds: x % y = (x & (y − 1)) . This is often many performant in many architectures. Can the same be done for mod 31 ? int mod31(int a){ return a % 31; }; 回答1: Here are two ways to approach this problem. The first one using a common bit-twiddling technique, and if carefully optimized can beat hardware division. The other one

Bitwise operator for simply flipping all bits in an integer?

眉间皱痕 提交于 2019-12-17 10:35:23
问题 I have to flip all bits in a binary representation of an integer. Given: 10101 The output should be 01010 What is the bitwise operator to accomplish this when used with an integer? For example, if I were writing a method like int flipBits(int n); , what would go in the body? I need to flip only what's already present in the number, not all 32 bits in the integer. 回答1: The ~ unary operator is bitwise negation. If you need fewer bits than what fits in an int then you'll need to mask it with &

A clear, layman's explanation of the difference between | and || in c#?

自古美人都是妖i 提交于 2019-12-17 10:26:23
问题 Ok, so I've read about this a number of times, but I'm yet to hear a clear, easy to understand (and memorable) way to learn the difference between: if (x | y) and if (x || y) ..within the context of C#. Can anyone please help me learn this basic truth, and how C# specifically, treats them differently (because they seem to do the same thing). If the difference a given piece of code has between them is irrelevant, which should I default to as a best-practise? 回答1: || is the logical-or operator.

Differences in boolean operators: & vs && and | vs ||

て烟熏妆下的殇ゞ 提交于 2019-12-16 20:17:28
问题 I know the rules for && and || but what are & and | ? Please explain these to me with an example. 回答1: Those are the bitwise AND and bitwise OR operators. int a = 6; // 110 int b = 4; // 100 // Bitwise AND int c = a & b; // 110 // & 100 // ----- // 100 // Bitwise OR int d = a | b; // 110 // | 100 // ----- // 110 System.out.println(c); // 4 System.out.println(d); // 6 Thanks to Carlos for pointing out the appropriate section in the Java Language Spec (15.22.1, 15.22.2) regarding the different

Why do we usually use `||` not `|`, what is the difference?

冷暖自知 提交于 2019-12-16 20:03:51
问题 I'm just wondering why we usually use logical OR || between two booleans not bitwise OR | , though they are both working well. I mean, look at the following: if(true | true) // pass if(true | false) // pass if(false | true) // pass if(false | false) // no pass if(true || true) // pass if(true || false) // pass if(false || true) // pass if(false || false) // no pass Can we use | instead of || ? Same thing with & and && . 回答1: If you use the || and && forms, rather than the | and & forms of

Writing integer values to a file in binary using C

烈酒焚心 提交于 2019-12-14 02:27:15
问题 I am trying to write 9 bit numbers to a binary file. For example, i want to write the integer value: 275 as 100010011 and so on. fwrite only allows one byte to be written at a time and I am not sure how to manipulate the bits to be able to do this. 回答1: You have to write a minimum of two bytes to store a 9-bits value. An easy solution is to use 16 bits per 9 bits value Choose a 16 bits unsigned type, eg uint16_t and store the 2 bytes uint16_t w = 275; fwrite(&w, 1, 2, myfilep); Reading the