bitwise-operators

Construction an logical expression which will count bits in a byte

北战南征 提交于 2019-12-22 04:09:19
问题 When interviewing new candidates, we usually ask them to write a piece of C code to count the number of bits with value 1 in a given byte variable (e.g. the byte 3 has two 1-bits). I know all the common answers, such as right shifting eight times, or indexing constant table of 256 precomputed results. But, is there a smarter way without using the precomputed table? What is the shortest combination of byte operations (AND, OR, XOR, +, -, binary negation, left and right shift) which computes

Why use xor with a literal instead of inversion (bitwise not)

瘦欲@ 提交于 2019-12-22 03:24:09
问题 I have come across this CRC32 code and was curious why the author would choose to use crc = crc ^ ~0U; instead of crc = ~crc; As far as I can tell, they are equivalent. I have even disassembled the two versions in Visual Studio 2010. Not optimized build: crc = crc ^ ~0U; 009D13F4 mov eax,dword ptr [crc] 009D13F7 xor eax,0FFFFFFFFh 009D13FA mov dword ptr [crc],eax crc = ~crc; 011C13F4 mov eax,dword ptr [crc] 011C13F7 not eax 011C13F9 mov dword ptr [crc],eax I also cannot justify the code by

how to use inverse in C

[亡魂溺海] 提交于 2019-12-22 02:06:19
问题 [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 回答1: Use XOR operator: Alpha.b = Alpha.a ^ 1; 回答2: In C, true is represented

Determine whether number is odd or even without using conditional code

你离开我真会死。 提交于 2019-12-21 17:38:30
问题 How to find whether a number is odd or even, without using if condition or ternary operators in Java? This question is given by my teacher. He also give me a hint that it is possible by using a bitwise operator. 回答1: There are few ways to not use if and get behavior that will be same as if if was used, like ternary operator condition ? valueIfTrue : valueIfFalse or switch/case . But to be tricky you can also use arrays and try to figure some transformation of our value to proper array index.

Why is there a distinction between logical and bitwise operators in Java and C#?

杀马特。学长 韩版系。学妹 提交于 2019-12-21 09:11:41
问题 Languages like i.e. Java and C# have both bitwise and logical operators. Logical operators make only sense with boolean operands, bitwise operators work with integer types as well. Since C had no boolean type and treats all non-zero integers as true, the existence of both logical and bitwise operators makes sense there. However, languages like Java or C# have a boolean type so the compiler could automatically use the right kind of operators, depending on the type context. So, is there some

What does the “&=” in this C# code do?

≡放荡痞女 提交于 2019-12-21 07:55:53
问题 I came across some code that looks like this: string someString; ... bool someBoolean = true; someBoolean &= someString.ToUpperInvariant().Equals("blah"); Why would I use the bitwise operator instead of "="? 回答1: It's not a bitwise operator when it's applied to boolean operators. It's the same as: someBoolean = someBoolean & someString.ToUpperInvariant().Equals("blah"); You usually see the short-cut and operator && , but the operator & is also an and operator when applied to booleans, only it

How to overload |= operator on scoped enum?

笑着哭i 提交于 2019-12-21 03:12:05
问题 How can I overload the |= operator on a strongly typed (scoped) enum (in C++11, GCC)? I want to test, set and clear bits on strongly typed enums. Why strongly typed? Because my books say it is good practice. But this means I have to static_cast<int> everywhere. To prevent this, I overload the | and & operators, but I can't figure out how to overload the |= operator on an enum . For a class you'd simply put the operator definition in the class, but for enums that doesn't seem to work

Ruby: What does the snippet: (num & 1) == 0 exactly do?

与世无争的帅哥 提交于 2019-12-20 09:58:46
问题 I was watching a metaprogramming video from PragProg, and Dave Thomas showed this snippet of code: module Math class << self def is_even?(num) (num & 1) == 0 # What exactly is going on here? Particularly (num & 1) end end end puts Math.is_even? 1 # => false puts Math.is_even? 2 # => true Now I understand what is going on here, but I don't know what exactly is happening with the (num & 1) part of the Math.is_even? class method. I know it is a bitwise operation but that is about it. Can someone

Is this how the + operator is implemented in C?

橙三吉。 提交于 2019-12-20 08:12:51
问题 When understanding how primitive operators such as + , - , * and / are implemented in C, I found the following snippet from an interesting answer. // replaces the + operator int add(int x, int y) { while(x) { int t = (x & y) <<1; y ^= x; x = t; } return y; } It seems that this function demonstrates how + actually works in the background. However, it's too confusing for me to understand it. I believed that such operations are done using assembly directives generated by the compiler for a long

Is this how the + operator is implemented in C?

↘锁芯ラ 提交于 2019-12-20 08:12:40
问题 When understanding how primitive operators such as + , - , * and / are implemented in C, I found the following snippet from an interesting answer. // replaces the + operator int add(int x, int y) { while(x) { int t = (x & y) <<1; y ^= x; x = t; } return y; } It seems that this function demonstrates how + actually works in the background. However, it's too confusing for me to understand it. I believed that such operations are done using assembly directives generated by the compiler for a long