bit-manipulation

C# Bitwise-or operator used on a sign-extended operand; consider casting to a smaller unsigned type first

南楼画角 提交于 2020-01-12 11:48:54
问题 I know these warnings are probably pointless.. But anyway I could get rid of them? I got 7 of these warnings. Bitwise-or operator used on a sign-extended operand; consider casting to a smaller unsigned type first This has something to do with the OR operator | I highlighted what gives off the warnings. int result = (int)ror((uint)(v76 ^ (v75 | 0x862D63D3)), (uint)(BitConverter.ToInt32(v4, 72) ^ 0x22)); int v11 = (int)rol((uint)(int)((v8 & v10 | ~v10 & 0xEFCDAAC9) + v3[2] - 1126481991), 17);

C hack for storing a bit that takes 1 bit space?

谁说我不能喝 提交于 2020-01-12 03:21:55
问题 I have a long list of numbers between 0 and 67600. Now I want to store them using an array that is 67600 elements long. An element is set to 1 if a number was in the set and it is set to 0 if the number is not in the set. ie. each time I need only 1bit information for storing the presence of a number. Is there any hack in C/C++ that helps me achieve this? 回答1: In C++ you can use std::vector<bool> if the size is dynamic (it's a special case of std::vector , see this) otherwise there is std:

How do bitwise operations work in Python?

扶醉桌前 提交于 2020-01-11 19:39:12
问题 I have been learning about Bitwise operations today and I learned that Not (~) inverses all bits, e.g.: 01010 to 10101 which means ~10 should be -5 but instead I have seen that it is -11 (per the python command line) which is 01010 to 11011 only two of the bits have been inverted. Can anybody explain why it isn't 10101? EDIT: After looking on my calculator I understand it a little better, But my own code for determining binary and ints is still being confused. Entering in (in byte mode)

Convert bit array to uint or similar packed value

喜欢而已 提交于 2020-01-11 09:23:06
问题 I have a large array of booleans, and I want to pack/unpack them into a uint or similar value. How can I do this in C#? 回答1: You can use the BitArray class to convert the bool array into an int array: int[] theIntArray = new int[(theBoolArray.Length + 31) / 32]; new BitArray(theBoolArray).CopyTo(theIntArray, 0); 回答2: You need to establish a series of bit masks (BIT0 to BIT31 e.g 2^0 and 2^32) then operate on them using bitwise operators: // Initialise byte value = 240; // 11110000 // Clear

Integer subtraction with wrap around for N bits

冷暖自知 提交于 2020-01-11 02:58:05
问题 Basically, the behavior you get when overflowing integers with subtraction, but for a given number of bits. The obvious way, assuming a signed integer: template <int BITS> int sub_wrap(int v, int s) { int max = (1<<(BITS)); v -= s; if (v < -max) v += max*2; // or if branching is bad, something like: // v += (max*2) * (v < -max) return v; } // For example subtracting 24 from -16 with 5 bit wrap, // with a range of -32, 31 sub_wrap<5>(-16, 28); -> 20 Is there a neat way of doing it that is less

Performing bit division without arithmetic operators [closed]

北城余情 提交于 2020-01-10 04:47:46
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . I am trying to complete an assignment that requires me to write three functions for binary arithmetic. badd() was provided for me, so I used it to help write the bsub() and bmult() functions. I am having trouble

Fastest way to produce a mask with n ones starting at position i

杀马特。学长 韩版系。学妹 提交于 2020-01-09 05:18:06
问题 What is the fastest way (in terms of cpu cycles on common modern architecture), to produce a mask with len bits set to 1 starting at position pos : template <class UIntType> constexpr T make_mask(std::size_t pos, std::size_t len) { // Body of the function } // Call of the function auto mask = make_mask<uint32_t>(4, 10); // mask = 00000000 00000000 00111111 11110000 // (in binary with MSB on the left and LSB on the right) Plus, is there any compiler intrinsics or BMI function that can help?

Shift N bits an entire array of chars

倖福魔咒の 提交于 2020-01-07 05:38:26
问题 Let's say I have an array of chars and I want to shift N bits to the left each byte, carrying to the left, so only the N bits of the first character would be lost. Example: kxmo shifted 3 bits to the left should become X@hx This is what I have currently, but it's not working as expected: #include <stdio.h> int main(void) { //shift the array with length *len* *shift* bits to the left int len = 4, shift = 3; unsigned char a[len] = "kxmo"; unsigned char b[len]; //X@hx unsigned char tmp = 0, tmp2

Filpping bits in binary number

与世无争的帅哥 提交于 2020-01-07 03:04:18
问题 Can anyone provide a clear logic for the below problem. I am stuck with confusion. An n bit number is given as input and OP(j) and OP(k) are applied on it one after the other. Objective is to specify how many bits will remain the same after applying these two operations. OP(i) implies flipping of each ith bit. i > 0 回答1: Based on your description, the operation OP(i) will change every 'i'th bit, so it changes a total of floor(n / i) bits. Chaining the operation makes things tricky. If the

Converting a C Checksum Function to Lua

纵然是瞬间 提交于 2020-01-06 19:35:10
问题 I am writing a script to allow my host device to send data files to a slave device. The slave requires a checksum calculation to be made and added to the end of my requests prior to sending the file(s). My problem is that not only am I fairly new to programming, but I'm still trying to fully grasp bit manipulation. I'm currently in a Java class so the checksum function so portions of the functions do have a familiar format, but since I'm still scratching my head with bits and the bit library,