bit-manipulation

Set Range of Bits in a ushort

最后都变了- 提交于 2019-12-14 04:18:08
问题 Lets say I have a ushort value that I would like to set bits 1 to 4 inclusive (assuming 0 is the LSB and 15 is the MSB). In C++ you could define a struct that mapped out specific bits: struct KibblesNBits { unsigned short int TheStart: 1; unsigned short int TheMeat: 4; unsigned short int TheRest: 11; } Then you could assign a value to 'TheMeat' directly. I'm looking to do something similar in C#. Ideally, I would like a funcion definition that looked like this: public ModValue SetRange

Kth element in transformed array

拥有回忆 提交于 2019-12-14 03:52:16
问题 I came across this question in recent interview : Given an array A of length N , we are supposed to answer Q queries. Query form is as follows : Given x and k , we need to make another array B of same length such that B[i] = A[i] ^ x where ^ is XOR operator. Sort an array B in descending order and return B[k] . Input format : First line contains interger N Second line contains N integers denoting array A Third line contains Q i.e. number of queries Next Q lines contains space-separated

Set or Reset a given bit without branching

老子叫甜甜 提交于 2019-12-14 03:45:02
问题 in one interview, they asked me, How do you set or reset a bit? This is a very simple question and I answered that. After that they asked me, do that without branching . I dont know what is branching. I search for that and I came here http://graphics.stanford.edu/~seander/bithacks.html But still not getting concept of branching and non-branching. Please explain Branching . 回答1: Branching means that the instructions the cpu executes contain a conditional jump. An either-or choice. Which could

UNDERSTANDING how to count trailing zeros for a number using bitwise operators in C

好久不见. 提交于 2019-12-14 03:36:56
问题 Note - This is NOT a duplicate of this question - Count the consecutive zero bits (trailing) on the right in parallel: an explanation? . The linked question has a different context, it only asks the purpose of signed() being use. DO NOT mark this question as duplicate. I've been finding a way to acquire the number of trailing zeros in a number. I found a bit twiddling Stanford University Write up HERE here that gives the following explanation. unsigned int v; // 32-bit word input to count

what happens if you cast a big int to float

懵懂的女人 提交于 2019-12-14 03:15:28
问题 this is a general question about what precisely happens when I cast a very big/small SIGNED integer to a floating point using gcc 4.4. I see some weird behaviour when doing the casting. Here are some examples: MUSTBE is obtained with this method: float f = (float)x; unsigned int r; memcpy(&r, &f, sizeof(unsigned int)); ./btest -f float_i2f -1 0x80800001 input: 10000000100000000000000000000001 absolute value: 01111111011111111111111111111111 exponent: 10011101 mantissa:

isGreater bitwise C manipulation - greatest of two numbers

烂漫一生 提交于 2019-12-14 03:13:46
问题 Bitwise manipulation and I have to find the greatest of two numbers. These are the rules for the same: /* * isGreater - if x > y then return 1, else return 0 * Example: isGreater(4,5) = 0, isGreater(5,4) = 1 * Legal ops: ! ~ & ^ | + << >> * Max ops: 24 */ This is the code I've written for this: int isGreater(int x, int y) { /* to find greater, subtract y from x using 2's complement method. * then find 2's complement of the answer and shift right by 31 to give MSB * which is 1 if x>y and 0 if

Probably simple bitwise shifting question

前提是你 提交于 2019-12-14 03:03:27
问题 unsigned short int temp, temp2; temp = 0xbc61; // 1011110001100001 // temp2 has to be 0x61 (01100001) but I don't know how to shift, mask or whatever temp2 = (temp << 8); // this doesnt work, because I get 0x6100 (110000100000000) 回答1: To select the 8 lowest bits of 1011110001100001 , you need to perform a bitwise AND operation with 11111111 : 1011110001100001 & 0000000011111111 ------------------ 0000000001100001 That is, temp2 = temp & 0xff; // or 255 来源: https://stackoverflow.com/questions

Packing 4 bytes into an int [duplicate]

泄露秘密 提交于 2019-12-14 02:57:10
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Convert 4 bytes to int I'm trying to pack 4 bytes into an int using some of the solutions found here, but it doesn't seem to work for one of my tests. This is the code I'm using: public static int pack(int c1, int c2, int c3, int c4) { return (c1 << 24) | (c2 << 16) | (c3 << 8) | (c4); } Now when I use it on something simple like 0x34, 0x68, 0x77, and 0x23 I get what I expect: 0x34687723. But when I use it on

Writing integer values to a file in binary using C

烈酒焚心 提交于 2019-12-14 02:27:15
问题 I am trying to write 9 bit numbers to a binary file. For example, i want to write the integer value: 275 as 100010011 and so on. fwrite only allows one byte to be written at a time and I am not sure how to manipulate the bits to be able to do this. 回答1: You have to write a minimum of two bytes to store a 9-bits value. An easy solution is to use 16 bits per 9 bits value Choose a 16 bits unsigned type, eg uint16_t and store the 2 bytes uint16_t w = 275; fwrite(&w, 1, 2, myfilep); Reading the

Ruby Unsigned Right Shift

不问归期 提交于 2019-12-13 18:25:17
问题 I have a snippet of java code: return (int)(seed >>> (48 - bits)); As you can see it uses the unsigned right shift operator (>>>). I'm trying to implement this code in ruby which has no unsigned right shift operator, only a signed right shift operator. As I'm not very familiar with the >>> operator I'm not quite sure on how I'd impelemnt this in ruby. I tried doing a few searches to see if anyone had come across this problem before, but couldn't find anything relevent. Any help would be much