About |= Bitwise Op | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

23rd Jul 2021, 11:57 AM
Kiwwi#
Kiwwi# - avatar
5 Answers
+ 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
23rd Jul 2021, 12:47 PM
Angelo
Angelo - avatar
+ 1
Kiwwi# do you mean using a = a | b compared to a |= b Or a = b compared to a |= b?
23rd Jul 2021, 1:43 PM
Angelo
Angelo - avatar
0
Angelo thx fot answer there's any good reason to use '|=' instead of '='
23rd Jul 2021, 1:30 PM
Kiwwi#
Kiwwi# - avatar
0
the second
24th Jul 2021, 1:33 PM
Kiwwi#
Kiwwi# - avatar
0
There is no reason to use |= (or += or *=) in place of =, because they do different things...
24th Jul 2021, 3:05 PM
Angelo
Angelo - avatar