bit-manipulation

Bit-wise operations to implement logical shift to the right [duplicate]

扶醉桌前 提交于 2019-12-20 07:40:26
问题 This question already has answers here : Implementing Logical Right Shift in C (8 answers) Closed 11 months ago . So I am trying to solve this home assignment and I have been stuck with this one particular problem for a couple of hours and can't figure it out. I feel like I am so close! But then i change something in the code and something else isn't right.. /* * logicalShift - shift x to the right by n, using a logical shift * Can assume that 0 <= n <= 31 * Examples: logicalShift(0x87654321

Bitwise NOT operator returning unexpected and negative value? [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-20 07:09:18
问题 This question already has answers here : Why is the output -33 for this code snippet (3 answers) Closed 5 years ago . I'm trying to get the value of an integer using Bitwise NOT, but i'm not getting what i expected. #include <stdio.h> int main(){ int i = 16; int j = ~i; printf("%d", j); return 0; } Isn't 16 supposed to be: 00000000000000000000000000010000 So ~16 is supposed to be: 11111111111111111111111111101111 Why i'm not getting what i expected and why the result is negative? This is what

Bitwise shift left and right in the same statement

落花浮王杯 提交于 2019-12-20 06:37:07
问题 Is char c2=i1<<8>>24; valid C syntax? (Where i1 is and unsigned integer) Additionally, will it yield the result of shifting i1 8 bits left and 24 bits right respectively? I am unpacking a char previously stored in i1 , along with three other chars. Code below: unsigned char b3 = 202; unsigned char b2 = 254; unsigned char b1 = 186; unsigned char b0 = 190; ... unsigned int i1=202; i1=i1<<8; i1=i1+254; i1=i1<<8; i1=i1+186; i1=i1<<8; i1=i1+190; ... char c1=i1>>24; char c2=i1<<8>>24; 回答1: The

Best way to store / retrieve bits C# [duplicate]

筅森魡賤 提交于 2019-12-20 05:00:55
问题 This question already has answers here : Best way to store long binary (up to 512 bit) in C# (5 answers) Closed 6 years ago . I am modifying an existing C# solution, wherein data is validated and status is stored as below: a) A given record is validated against certain no. of conditions (say 5). Failed / passed status is represented by a bit value (0 - passed; 1 - failed) b) So, if a record failed for all 5 validations, value will be 11111. This will be converted to a decimal and stored in a

How to convert a sequence of 32 char (0/1) to 32 bits (uint32_t)?

≡放荡痞女 提交于 2019-12-20 04:26:07
问题 I have an array of char (usually thousands of bytes long) read from a file, all composed of 0 and 1 (not '0' and '1', in which case I could use strtoul ). I want to pack these into single bits, thus converting each 32 char into a single uint32_t. Should I write a bit shift operation with 32 parts, or is there a saner way? out[i/32] = data[i] << 31 | data[i+1] << 30 | data[i+2] << 29 | data[i+3] << 28 | data[i+4] << 27 | data[i+5] << 26 | data[i+6] << 25 | data[i+7] << 24 | data[i+8] << 23 |

Unsigned right shift function not working for negative input

白昼怎懂夜的黑 提交于 2019-12-20 04:20:49
问题 I'm looking for a way to use the >>> function from JavaScript in the 64-bit version of PHP 5.5.14. I found this function in my googling: function uRShift($a, $b) { $z = hexdec(80000000); if ($z & $a) { $a = ($a >> 1); $a &= (~$z); $a |= 0x40000000; $a = ($a >> ($b - 1)); } else { $a = ($a >> $b); } return $a; } This function seems to work fine for positive numbers, but I get differing results when passing in negative numbers. For example: PHP: In: echo uRShift(-672461345, 25); Out: -149

How to convert from sign-magnitude to two's complement

╄→гoц情女王★ 提交于 2019-12-20 04:05:19
问题 How would I convert from sign-magnitude to two's complement. I don't know where to start. Any help would be appreciated. I can only use the following operations:!,~,|,&,^,+,>>,<<. /* * sm2tc - Convert from sign-magnitude to two's complement * where the MSB is the sign bit * Example: sm2tc(0x80000005) = -5. * */ int sm2tc(int x) { return 2; } 回答1: You can convert signed-magnitude to two's complement by subtracting the number from 0x80000000 if the number is negative. This will work for a 32

How to convert from sign-magnitude to two's complement

﹥>﹥吖頭↗ 提交于 2019-12-20 04:05:00
问题 How would I convert from sign-magnitude to two's complement. I don't know where to start. Any help would be appreciated. I can only use the following operations:!,~,|,&,^,+,>>,<<. /* * sm2tc - Convert from sign-magnitude to two's complement * where the MSB is the sign bit * Example: sm2tc(0x80000005) = -5. * */ int sm2tc(int x) { return 2; } 回答1: You can convert signed-magnitude to two's complement by subtracting the number from 0x80000000 if the number is negative. This will work for a 32

How to set and clear different bits with a single line of code (C)

夙愿已清 提交于 2019-12-20 03:12:17
问题 data |= (1 << 3) sets bit (3) without disrupting other bits. data &= ~(1 << 4) resets bit (4) without disrupting other bits. How can I accomplish both tasks in a single instruction? (As this is really only for readability, I plan on #define ing this in a cute way like #define gpioHigh(x) <insert code> . The alternative is to figure out how to correctly pass a gpio pointer into functions that I write expressly for this purpose, but eff that) Thanks! Mike 回答1: It's not possible in a single

Extracting bits using bit manipulation

橙三吉。 提交于 2019-12-20 03:06:08
问题 I have a 32-bit unsigned int and I need to extract bits at given positions and make a new number out of those bits. For example, if I have a 0xFFFFFFFF and want bits 0,10,11 my result will be 7 (111b). This is my attempt, it extracts the bits correctly but doesn't create the correct result. I'm shifting the result 1 place left and ANDing it with my extracted bit, apparenlty this is incorrect though? I'm also sure there is probably a much more elegant way to do this? #define TEST 0xFFFFFFFF