bit-manipulation

Why does shifting 0xff left by 24 bits result in an incorrect value?

南楼画角 提交于 2019-12-13 09:08:04
问题 I would like to shift 0xff left by 3 bytes and store it in a uint64_t , which should work as such: uint64_t temp = 0xff << 24; This yields a value of 0xffffffffff000000 which is most definitely not the expected 0xff000000 . However, if I shift it by fewer than 3 bytes, it results in the correct answer. Furthermore, trying to shift 0x01 left by 3 bytes does work. Here's my output: 0xff shifted by 0 bytes: 0xff 0x01 shifted by 0 bytes: 0x1 0xff shifted by 1 bytes: 0xff00 0x01 shifted by 1 bytes

Why is 1 plus 31 zeros not a valid integer, if bit flags are 31 length, plus one negative/positive flag?

拥有回忆 提交于 2019-12-13 08:09:55
问题 According to this, in the around-fourth paragraph below the first table, says In general, we will use an integer to represent a set on a domain of up to 32 values (or 64, using a 64-bit integer), with a 1 bit representing a member that is present and a 0 bit one that is absent. If that's true, then why does this 32 digit binary number exceed Integer.MAX_VALUE ? System.out.println(Integer.parseInt("10000000000000000000000000000000", 2)); Error: Exception in thread "main" java.lang

How Do you reset a bit value in a string?

爷,独闯天下 提交于 2019-12-13 08:00:47
问题 In the recent interview I got a question like this : Given a string value, find out its 127th bit and reset it, do this in C language Reset means if that particular bit is 0 change to 1 and vice versa I didn't find out any algorithm for this, but I want to know about how one could solve this in C language. Edit: After getting the answer from few, I tried this : #include<stdio.h> void main() { char *str="anto"; str[15] ^= 0x80; printf("%s",str); } I get the output as : anto . Now I got strike

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

Carry bits in incidents of overflow

痞子三分冷 提交于 2019-12-13 07:07:21
问题 /* * isLessOrEqual - if x <= y then return 1, else return 0 * Example: isLessOrEqual(4,5) = 1. * Legal ops: ! ~ & ^ | + << >> * Max ops: 24 * Rating: 3 */ int isLessOrEqual(int x, int y) { int msbX = x>>31; int msbY = y>>31; int sum_xy = (y+(~x+1)); int twoPosAndNegative = (!msbX & !msbY) & sum_xy; //isLessOrEqual is FALSE. // if = true, twoPosAndNegative = 1; Overflow true // twoPos = Negative means y < x which means that this int twoNegAndPositive = (msbX & msbY) & !sum_xy;//isLessOrEqual

how to understand i & -i in python? bit manipulation in python

隐身守侯 提交于 2019-12-13 07:01:42
问题 I find '&' in python means 'and' operation based on bit expression. Recently, I find a very smart code and one line is like 'i & -i' where i is a integer. How to understand the result of 'i & -i'. In addition, how python deal with negative integer '-i' for bit manipulation? 回答1: "i & -i" - this is clear all bits "1", but last significant one. For example: i = 10(dec) = 000010 1 0(bin) i & -i will be 000000 1 0(bin) = 2(dec) By math terms, "i & -i" returns maximal 2^N, which is divider of "i".

Binary to unsigned int using bitwise operations and pointer arithmetic in C

半世苍凉 提交于 2019-12-13 05:50:00
问题 I can only use bitwise operations and pointer arithmetic to solve this problem. I am converting from binary to unsigned int. The function I am writing is: unsigned int atob(const char* nptr); atob("101") should return 5, atob("11000") should return 24, atob("11$") should return 3, and atop("") should return 0. I'm pretty new to bitwise operations, so I really need some help specifically in that area. edit: nptr can only be incremented, not other inc/dec's are allowed. 回答1: unsigned bits2val

checking the number of bits ON in a byte?

走远了吗. 提交于 2019-12-13 05:16:53
问题 I know we can set any bit in the byte by using logical OR and can clear any bit by logical AND like to val |= (1<<order_number_of_bit_to_set); //for setting some specific number of bit and for clearing a bit val &= ~(1<<order_number_of_bit_to_clear); // specific bit to clear but my question is how can we check that how many and which ordered number bits are set in the byte. for example if we have val = 0x22; it means that 2nd and 5th bit is set in the byte what is the efficient, quick and

Shifting negative BigInteger value - Java

試著忘記壹切 提交于 2019-12-13 05:13:23
问题 I am trying to shift a 7 byte array to the right by 7 bits. To do this, I am using BigInteger's shiftright method. However, when shifting right for negative BigIntegers, padding with 1s are added or sometimes the leading bit is removed. Here is the following bit of code doing the shifting: byte[] vcwManD = decryptedVCW; BigInteger bigIntD = new BigInteger(vcwManD); // create big int array for shift BigInteger shiftIntD= bigIntD.shiftRight(7); // shift right 7 bits vcwManD = shiftIntD

C# Storing categories in a value - Bitmasks

偶尔善良 提交于 2019-12-13 04:55:00
问题 I am working on an application that have some categories. The aim is to store the categories in a value. First, I choosed to store them in an int. Categories : 0, 1, 2, 3... Then I do a bitmask operation to find which category was selected. But the problem is that I can't store more that 31 categories is this int. Is there any way to make such system ? I don't want to go threw the unlimited number of categories but maybe over 64. The target language is C# but any other solution could be fine.