0
About |= Bitwise Op
can explain why use: int var |= 0x01 instead of: int var |= 1 or that: int var = 1 it has some performance advantages?
5 Antworten
+ 1
= is different from |=
Let's say var is just 1 byte and its value is (in binary) 0b00100100
var = 1 -> 0b00000001
var |= 1 -> 0b00100101
Using decimal instead of exadecimal (or octal or binary) doesn't make any difference in performance because the compiler translates it in any case
I personally prefer (when dealing with bitwise operators) to use either exadecimal or binary
Let's say you want to switch the first and last bit of var to 1
Using decimal you would calculate 2^7+1 and than, every time you need to know what bits you are switching, translate 129 in binary
On the other hand using binary you have everything ready (but a little bit too long)
Using exadecimal you have
0x81 (8 is 1000 and 1 is 0001) which is shorter and as easy to read and compute
+ 1
Kiwwi# do you mean using
a = a | b compared to a |= b
Or a = b compared to a |= b?
0
Angelo thx fot answer
there's any good reason to use '|=' instead of '='
0
the second
0
There is no reason to use |= (or += or *=) in place of =, because they do different things...