bit-manipulation

How to tell if a 32 bit int can fit in a 16 bit short

橙三吉。 提交于 2019-12-19 06:18:35
问题 Using only: ! ~ & ^ | + << >> I need to find out if a signed 32 bit integer can be represented as a 16 bit, two's complement integer. My first thoughts were to separate the MSB 16 bits and the LSB 16 bits and then use a mask to and the last 16 bits so if its not zero, it wont be able to be represented and then use that number to check the MSB bits. An example of the function I need to write is: fitsInShort(33000) = 0 (cant be represented) and fitsInShort(-32768) = 1 (can be represented) 回答1:

How would you set and clear a single bit in Go?

匆匆过客 提交于 2019-12-19 05:11:34
问题 In Golang, how do you set and clear individual bits of an integer? For example, functions that behave like this: clearBit(129, 7) // returns 1 setBit(1, 7) // returns 129 回答1: Here's a function to set a bit. First, shift the number 1 the specified number of spaces in the integer (so it becomes 0010, 0100, etc). Then OR it with the original input. This leaves the other bits unaffected but will always set the target bit to 1. // Sets the bit at pos in the integer n. func setBit(n int, pos uint)

Command to pad zeros to specific locations in binary numbers?

别说谁变了你拦得住时间么 提交于 2019-12-19 04:23:58
问题 I need to pad zeros to specific locations in binary numbers. Looping the array form of a binary number such as dec2bin(43) and adding the zeros and adjusting the size sounds reinvention of the wheel. How to pad zeros efficiently to binary numbers in Matlab? Looping positions=[1,3,6]; x=de2bi(43); xx=flip(x); kk=1; for ii=1:length(x)+length(positions) if isequal(positions(kk),xx(ii)) %Transfer the tail from ii by one index ahead, %left out here because I think my method is bad! x(ii)=0;

PHP bitwise left shifting 32 spaces problem and bad results with large numbers arithmetic operations

∥☆過路亽.° 提交于 2019-12-19 04:14:49
问题 I have the following problems: First: I am trying to do a 32-spaces bitwise left shift on a large number, and for some reason the number is always returned as-is. For example: echo(516103988<<32); // echoes 516103988 Because shifting the bits to the left one space is the equivalent of multiplying by 2, i tried multiplying the number by 2^32, and it works, it returns 2216649749795176448. Second: I have to add 9379 to the number from the above point: printf('%0.0f', 2216649749795176448 + 9379);

Why this union's size is 2 with bitfields?

风流意气都作罢 提交于 2019-12-19 04:07:10
问题 I am working on turbo C on windows where char takes one byte.Now my problem is with the below union. union a { unsigned char c:2; }b; void main() { printf("%d",sizeof(b)); \\or even sizeof(union a) } This program is printing output as 2 where as union should be taking only 1 byte. Why is it so? for struct it is fine giving 1 byte but this union is working inappropriately. And one more thing how to access these bit fields. scanf("%d",&b.c); //even scanf("%x",b.c); is not working because we

How to Bitwise compare a String

风流意气都作罢 提交于 2019-12-19 03:44:09
问题 I am working on a function that takes in a series of permission strings, less than 255 characters and assigns them to an entity. Each string assigned is unique, but there are so many that dropping them into an array, serializing them and pushing into a database and pulling them out later and de-serializing them or recalculating from a query every time there is a load has been causing delay problems. Especially with inherited permissions. So I was thinking of taking the string, generating a

Reading characters on a bit level

冷暖自知 提交于 2019-12-19 03:39:05
问题 I would like to be able to enter a character from the keyboard and display the binary code for said key in the format 00000001 for example. Furthermore i would also like to read the bits in a way that allows me to output if they are true or false. e.g. 01010101 = false,true,false,true,false,true,false,true I would post an idea of how i have tried to do it myself but I have absolutely no idea, i'm still experimenting with C and this is my first taste of programming at such a low level scale.

Is there a practical limit to the size of bit masks?

早过忘川 提交于 2019-12-19 03:13:29
问题 There's a common way to store multiple values in one variable, by using a bitmask. For example, if a user has read, write and execute privileges on an item, that can be converted to a single number by saying read = 4 (2^2), write = 2 (2^1), execute = 1 (2^0) and then add them together to get 7. I use this technique in several web applications, where I'd usually store the variable into a field and give it a type of MEDIUMINT or whatever, depending on the number of different values. What I'm

Bitwise rotation (Circular shift)

主宰稳场 提交于 2019-12-19 03:11:13
问题 I was trying to make some code in C++ about “bitwise rotation” and I would like to make this by the left shif. I didn’t know how to code this, but I found a little code in “Wikipedia” like this. unsigned int rotl(unsigned int value, int shift) { return (value << shift) | (value >> (sizeof(value) * CHAR_BIT - shift)); } Then I tried to make it work, but this code don’t give the output that I expected. Ex. I have the number unsigned int 12 , in binary 1100, and when I want to do bitwise

How can I combine 4 bytes into a 32 bit unsigned integer?

喜你入骨 提交于 2019-12-19 02:18:06
问题 I'm trying to convert 4 bytes into a 32 bit unsigned integer. I thought maybe something like: UInt32 combined = (UInt32)((map[i] << 32) | (map[i+1] << 24) | (map[i+2] << 16) | (map[i+3] << 8)); But this doesn't seem to be working. What am I missing? 回答1: Your shifts are all off by 8. Shift by 24, 16, 8, and 0. 回答2: Use the BitConverter class. Specifically, this overload. 回答3: BitConverter.ToInt32() You can always do something like this: public static unsafe int ToInt32(byte[] value, int