bitwise-operators

C#: Shift left assignment operator behavior

核能气质少年 提交于 2019-12-05 08:40:42
I'm running code that sometimes yields this: UInt32 current; int left, right; ... //sometimes left == right and no shift occurs current <<= (32 + left - right); //this works current <<= (32 - right); current <<= left; It appears for any value >= 32, only the value % 32 is shifted. Is there some "optimization" occurring in the framework? C# 3.0 language specification, 7.8 "Shift operators": For the predefined operators, the number of bits to shift is computed as follows: When the type of x is int or uint, the shift count is given by the low-order five bits of count. In other words, the shift

~ bitwise operator in JavaScript

穿精又带淫゛_ 提交于 2019-12-05 08:29:43
I have the following code : var a = parseInt('010001',2); console.log(a.toString(2)); // 10001 var b = ~a; console.log(b.toString(2)); // -10010 The MSDN Say ~ Performs the NOT operator on each bit. NOT a yields the inverted value (a.k.a. one's complement) of a. 010001 should thus return this 101110 . This Topic kinda confirm that So I can't understand how we can get -10010 instead ? The only potential explanation is that: 010001 is negated 101110 but he write this -10001 and then for an obscure reason he give me the two complements and -10001 become -10010. But all this is very obscure in my

To divide by 2 which one is better Right Shift Operator or traditional division operator? [duplicate]

做~自己de王妃 提交于 2019-12-05 08:00:22
问题 This question already has answers here : Right Shift to Perform Divide by 2 On -1 (6 answers) Closed 5 years ago . While reading Java Source code for Collections.reverse method, Right Shift operator is used for finding middle. ...... for (int i=0, mid=size>>1, j=size-1; i<mid; i++, j--) // Right Shift swap(list, i, j); ..... Same can be done by using traditional divide by 2 approach. I explored on stack Right Shift to perform division and find that its better to use division operator and not

Are bitwise operations still practical?

人走茶凉 提交于 2019-12-05 07:16:03
Wikipedia, the one true source of knowledge, states: On most older microprocessors, bitwise operations are slightly faster than addition and subtraction operations and usually significantly faster than multiplication and division operations. On modern architectures, this is not the case: bitwise operations are generally the same speed as addition (though still faster than multiplication). Is there a practical reason to learn bitwise operation hacks or it is now just something you learn for theory and curiosity? Bitwise operations are worth studying because they have many applications. It is

What is C# exclusive or `^` usage? [closed]

那年仲夏 提交于 2019-12-05 06:24:54
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . Can anyone explain this operator with a good example? I know what this operator is. I mean a real-life example. It is an implementation of the the logical operation exclusive disjunction http://en.wikipedia.org/wiki/Exclusive_or

Xor encryption in PHP

六眼飞鱼酱① 提交于 2019-12-05 03:45:15
I'm new to Xor encryption, and I'm having some trouble with the following code: function xor_this($string) { // Let's define our key here $key = ('magic_key'); // Our plaintext/ciphertext $text =$string; // Our output text $outText = ''; // Iterate through each character for($i=0;$i<strlen($text);) { for($j=0;$j<strlen($key);$j++,$i++) { $outText .= $text{$i} ^ $key{$j}; //echo 'i='.$i.', '.'j='.$j.', '.$outText{$i}.'<br />'; //for debugging } } return $outText; } When I run this it works for normal strings, like 'dog' but it only partially works for strings containing numbers, like '12345'.

-Wconversion warning while using operator <<= on unsigned char

不羁岁月 提交于 2019-12-05 02:16:02
When I compile the following code with gcc : int main() { unsigned char c = 1; c <<= 1; // WARNING ON THIS LINE return 0; } I get this warning : conversion to ‘unsigned char’ from ‘int’ may alter its value [-Wconversion] Why ? What is wrong with this code ? Actually, can I really use the oprator <<= on a unsigned char variable ? Compilation command : g++ test.cpp -Wconversion -o test.exe This is a valid warning: c <<= 1; is equivalent to: c = c << 1 and the rules for << say that the operands are promoted and in this case will be promoted to int and the result is of the promoted type. So there

Rounded division by power of 2

时光怂恿深爱的人放手 提交于 2019-12-05 00:17:36
I'm implementing a quantization algorithm from a textbook. I'm at a point where things pretty much work, except I get off-by-one errors when rounding. This is what the textbook has to say about that: Rounded division by 2^p may be carried out by adding an offset and right-shifting by p bit positions Now, I get the bit about the right shift, but what offset are they talking about? Here's my sample code: def scale(x, power2=16): if x < 0: return -((-x) >> power2) else: return x >> power2 def main(): inp = [ 12595827, -330706, 196605, -387168, -274244, 377496, -241980, -545272, -196605, 24198,

how to use inverse in C

荒凉一梦 提交于 2019-12-04 22:33:34
[how to use ~ operator ] I have a structure say Alpha . I know the value of element inside Alpha (say a ) which can be 0 or 1 - I want the other element of same structure to take inverse value of Alpha.a. For example: if Alpha.a = 1 then Alpha.b = 0 and vice versa I have tried: Alpha.b = ~ (Alpha.a) But unfortunately it doesnt work - when Alpha.a is 1 , Alpha.b gets set to 254 Any ideas? Thanks and regards, SamPrat Use XOR operator: Alpha.b = Alpha.a ^ 1; In C, true is represented by 1, and false by 0. However, in a comparison, any non-false value is treated is true. The ! operator does

How to use bitwise operators to return a 0 or 1

☆樱花仙子☆ 提交于 2019-12-04 21:08:18
My function takes in a 32 bit int and I need to return a 0 or 1 if that number has a 1 in any even position. I cant use any conditional statements I also can only access 8 bits at a time. Here is an example input: 10001000 01011101 00000000 11001110 1) Shift the bits and and them with AA(10101010) and store each one in a variable. int a = 10001000 int b = 1000 int c = 0 int d = 10001010 Now I need to return a 0 if there were no odd bits set and 1 if there were. As we can see there were. So I need to combine these into one number and then use the !! operator to return 0 or 1. This is where I am