bitwise-operators

Bitwise operations | And | Which situation is true or false in c#

岁酱吖の 提交于 2019-12-13 22:51:52
问题 I show my question by an example : int a = 1 << 0; // = 1 int flag = 1; bool b = flag & a; // = 1 < In c++ this line has no error but in c# error is like this : Cannot be applied to operands of type 'bool' and 'int' When b variable is true and when b variable is false in c#? How fix the error? When does c++ recognize that b variable is true? The other side should be (flag & a) != 0 or (flag & a) == 1 or something else? 回答1: In C# you write it like so: bool b = (flag & a) != 0; You can't

using bitwise operation to extract month from int date (yyyyMMdd)

亡梦爱人 提交于 2019-12-13 22:40:01
问题 Is it possible to extract the month from date represented as int (format YYYYMMDD, e.g. 20110401) using some bitwise operators? If so, how can it be done? edit: I am currently using 20110401 % 10000 / 100. I thought bit-wise could be faster. DateTime.Parse etc. are too slow for what I am trying to do. 回答1: You could efficiently extract the month with bitwise operations if you represented the date in a binary format, e.g., 5 bits for the day of the month, 4 bits for the month number, and the

Field packing to form a single byte

好久不见. 提交于 2019-12-13 19:41:08
问题 I'm struggling to learn how to pack four seperate values into a single byte. I'm trying to get a hex output of 0x91 and the binary representation is supposed to be 10010001 but instead I'm getting outputs of: 0x1010001 and 16842753 respectively. Or is there a better way to do this? uint8_t globalColorTableFlag = 1; uint8_t colorResolution = 001; uint8_t sortFlag = 0; uint8_t sizeOfGlobalColorTable = 001; uint32_t packed = ((globalColorTableFlag << 24) | (colorResolution << 16) | (sortFlag <<

Bit fields for reading from H/W registers

社会主义新天地 提交于 2019-12-13 14:32:47
问题 I want to read the 2nd, 5th and the 6th bit from a 32-bit register. I have decided to use struct bit fields to store them. Is the following data structure correct? struct readData { int unwanted:1; int reqbit1:1; int unwanted1:2; int reqbit2:2; int unwanted2:26; }; I'm not sure about how the bit fields are created. I'm going to use an API that will copy bytes from the h/w register to this structure directly. In that case, will reqbit1 contain the 2nd bit? As per my understanding, the compiler

Why a positive number operated with bitwise or 0 not always positive in Javascript

为君一笑 提交于 2019-12-13 14:26:29
问题 Why a positive number operated with bitwise or 0 not always positive in Javascript For example: 3391700000|0 -903267296 4260919000|0 -34048296 2884900000|0 -1410067296 I'm using chrome 64-bit on Linux related to: https://stackoverflow.com/a/12837315/1620210 回答1: Because JavaScript uses 32bit integers at most, but keep in mind every number is kind of a float in this language If you want to truncate them to an unsigned 32bit value: (3391700000|0) >>> 0 回答2: In JavaScript, the operands of

meaning of & in C++

我们两清 提交于 2019-12-13 08:58:45
问题 I am bit confused about a expression I found in some C++ code: if (m & 1) pmm=-pmm; I am not a C/C++ coder, so Google gives me two things: & is bitwise AND if syntax is if (condition) statement So, how does the above statement work? Should I not require a if ((m & 1)==0) ? 回答1: Just to add to the more technical explanations, a simpler way of viewing if (m & 1) is that is tests whether m is odd (i.e. not an exact multiple of 2): if (m & 1) // m is odd (1, 3, 5, 7, 9, ...) - do something else /

Bitwise Operations — Arithmetic Operations

ぃ、小莉子 提交于 2019-12-13 08:58:32
问题 Can you please explain the below lines, with some good examples. A left arithmetic shift by n is equivalent to multiplying by 2 n (provided the value does not overflow). And: A right arithmetic shift by n of a two's complement value is equivalent to dividing by 2 n and rounding toward negative infinity. If the binary number is treated as ones' complement, then the same right-shift operation results in division by 2 n and rounding toward zero. 回答1: I will explain what happens in a base that we

forcing usage of bitwise and instead of boolean and

徘徊边缘 提交于 2019-12-13 08:35:50
问题 In C++, a bool is guaranteed to be 0 or 1 C++ (§4.5/4): An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true becoming one. Consider the following function and what g++5.2 generates with -O3 int foo(bool a, bool b) { if(a&b) return 3; else return 5; } 0000000000000000 <_Z3foobb>: 0: 40 84 ff test %dil,%dil 3: 74 13 je 18 <_Z3foobb+0x18> 5: 40 84 f6 test %sil,%sil 8: b8 03 00 00 00 mov $0x3,%eax d: 74 09 je 18 <_Z3foobb+0x18> f: f3 c3 repz retq 11:

Checking bits of ints to see if they share binary with the power of 2 (bitwise only)

落爺英雄遲暮 提交于 2019-12-13 07:59:49
问题 I'm trying to recreate this function: int test(int x) { int i; for (i = 0; i < 32; i+=2) if ((x & (1<<i)) == 0) return 0; return 1; } But only using these bit-wise operators: !, ~, &, ^, |, +, <<, and >> (Meaning no loops or if statements either) I am so confused with this question I have been staring at it for like a hour and am still not sure where to start. I understand that basically it is taking x comparing it with 2^i where i is 0-31 and then returning 0 if x and 2^i do not share any of

bitwise and logical AND/OR in terms of hex result

让人想犯罪 __ 提交于 2019-12-13 03:06:12
问题 so if I have x = 0x01 and y = 0xff and I do x & y , I would get 0x01 . If I do x && y do I still get 0x01 , but the computer just says its true if its anything than 0x00 ? My question is are the bits the same after the operation regardless of bit-wise or logical AND/OR, but the computer just interprets the final result differently? In other words are the hex values of the result of & and && (likewise | and ||) operations the same? edit: talking about C here 回答1: The answer depends on the