bit-manipulation

Change sign using bitwise operators

谁都会走 提交于 2019-12-02 07:46:17
问题 How to change the sign of int using bitwise operators? Obviously we can use x*=-1 or x/=-1 . Is there any fastest way of doing this? I did a small test as below. Just for curiosity... public class ChangeSign { public static void main(String[] args) { int x = 198347; int LOOP = 1000000; int y; long start = System.nanoTime(); for (int i = 0; i < LOOP; i++) { y = (~x) + 1; } long mid1 = System.nanoTime(); for (int i = 0; i < LOOP; i++) { y = -x; } long mid2 = System.nanoTime(); for (int i = 0; i

C# Bitwise Operator With Ints

落爺英雄遲暮 提交于 2019-12-02 07:27:16
What does this expression actually mean?? Note - the x and y vars are just sample values. int x = 3; int y = 1; if ((x & y) !=0) I inherited a codebase and am not up to speed on bitwise operators. I have read up, but am still missing something. Help! Joe It's comparing the bits in each value. It returns any bits that are set in both numbers. In your example: 3: 0011 1: 0001 3 & 1: 0001 This checks whether x and y both have at least one common bit set. In the case of your example this would be the true. driis if ((x & y) != 0) This would typically be used to determine whether the value x has a

How assignment from int to char works in C?

三世轮回 提交于 2019-12-02 06:51:01
问题 What happens when you assign an int to a char in C? Does it always just ignore the extra bits on the left? Example (4 bytes int): unsigned char c = 0; unsigned int i = 500; c = i; // c is 244 c = i << 24 >> 24; //c is 244 i = i << 24 >> 24; //i is 244 In binary, 500 is 111110100 and 244 is 11110100 . 回答1: Typically, this is exactly what happens. Section 6.3.1.3 of the ISO/IEC 9899:2011 standard prescribes what has to happen in this case: 6.3.1.3 Signed and unsigned integers When a value with

Problem with Bitwise Barrel Shift Rotate left and right in C#

和自甴很熟 提交于 2019-12-02 06:48:55
问题 In C++ I have code like this. static UInt32 rol(UInt32 value, UInt32 bits) { bits &= 31; return ((value << bits) | (value >> (32 - bits))); } static UInt32 ror(UInt32 value, UInt32 bits) { bits &= 31; return ((value >> bits) | (value << (32 - bits))); } how would it look in C#? I think the same exact way.. only problem Error 2 Operator '>>' cannot be applied to operands of type 'uint' and 'uint' Error 3 Operator '>>' cannot be applied to operands of type 'uint' and 'uint' Error 1 Operator '<<

Why does 3,758,096,384 << 1 give 768

匆匆过客 提交于 2019-12-02 06:34:28
After reading the great answer for Absolute Beginner's Guide to Bit Shifting? I tested the claim (sic): 3,758,096,384 << 1 from Chrome console: 3,758,096,384 << 1 > 768 3,758,096,384 << 2 > 1536 3758096384 << 1 > -1073741824 That's the comma operator at work. It's actually 384 << 1 . (The comma operator evaluates its left hand side, then evaluates its right hand side, and returns the right hand side.) It returns 768 because you're incorrectly using the comma operator. 3,758,096,384 << 1 will actually be 384 << 1 because the comma operator will return the last operand. 来源: https://stackoverflow

Why does << 32 not result in 0 in javascript?

情到浓时终转凉″ 提交于 2019-12-02 06:32:48
问题 This is false: (0xffffffff << 31 << 1) === (0xffffffff << 32) It seems like it should be true. Adding >>> 0 anywhere does not change this. Why is this and how can I correctly write code that handles << 32 ? 回答1: The shift operators always effectively has a right operand in the range 0-31. From the Mozilla docs: Shift operators convert their operands to 32-bit integers in big-endian order and return a result of the same type as the left operand. The right operand should be less than 32, but if

Easiest way to convert a decimal float to bit representation manually based on IEEE 754, without using any library

ⅰ亾dé卋堺 提交于 2019-12-02 05:54:12
问题 I know there are number ways to read every bit of a IEEE 754 float using written libraries. I don't want that, and I want to be able to manually convert a decimal float to binary representation based on IEEE 754. I understand how IEEE 754 works and I am just trying to apply it. I ask this question here just want to see whether my way is normal or stupid and I am also wondering how PC does it quickly. If I am given a decimal float in a string , I need to figure out what the E is and what the M

Swapping bits in a positive 32bit integer in C#

江枫思渺然 提交于 2019-12-02 05:47:19
问题 So I'm trying to solve this problem: You are given random 32bit positive integer, and what you have to do is swap the values of the bits on 3rd, 4th and 5th positions with those on 24th, 25th and 26th position. 回答1: Assuming that this is a problem for which you do not want an explicit solution, here is a hint: mask the bits in question using & , do a shift, and then OR then in using bitwise | . You can "cut out" bits 3, 4, and 5 using the 0x00000034 mask, and bits 24, 25, and 26 using the

Handling arbitrary bit length data in Delphi?

試著忘記壹切 提交于 2019-12-02 05:33:32
问题 I am working on a display/control utility to replace an ancient dedicated hardware controller for a piece of industrial machinary. The controller itself is beyond repair (someone replaced the 1 amp fuse with a 13 amp one "because it kept blowing"). The hardware interface is through a standard RS232 port. The data format is dedicated: No control characters are used with the exeption of ETB (Chr 23) to demark end of a message. The data is 7-bit, but only a subset of the possible 7-bit

Why is the output -33 for this code snippet

夙愿已清 提交于 2019-12-02 05:24:59
问题 #include<stdio.h> int main() { int a=32; printf("%d\n", ~a); //line 2 return 0; } o/p = -33 Actually in the original snippet line 2 was printf("%x\n", ~a); //line 2 I solved it like 32 in hex is 20. 0000 0000 0010 0000 now tilde operator complements it 1111 1111 1101 1111 = ffdf. I am confused how to solve it when I have printf("%d\n", ~a); //line 2 i.e %d NOT %x. 回答1: In your C implementation, as in most modern implementations of any programming language, signed integers are represented with