bit-shift

shifting a binary number to the right in assembly with SAR vs. SHR

可紊 提交于 2019-12-02 00:42:09
问题 We know that when we shift a binary number to the right, it is divided by 2. For the number: 1001 0001 . Let's assume that is stored in AX register: If we consider it as unsigned number (which is equal to 145 in decimal): SHR AX, 1 will make AX equal to 0100 1000 which is equal to 72 in decimal, and this is right. But if we consider it as signed number (which is equal to -111 in decimal): SAR AX, 1 will make AX equal to 1100 0100 which is equal to -56 in decimal, and this is wrong because it

Weird behaviour of bit-shifting with byte in Java

和自甴很熟 提交于 2019-12-01 22:14:16
问题 As I was using bit-shifting on byte , I notice I was getting weird results when using unsigned right shift ( >>> ). With int , both right shift (signed: >> and unsigned: >>> ) behave as expected: int min1 = Integer.MIN_VALUE>>31; //min1 = -1 int min2 = Integer.MIN_VALUE>>>31; //min2 = 1 But when I do the same with byte , strange things happen with unsigned right shift: byte b1 = Byte.MIN_VALUE; //b1 = -128 b1 >>= 7; //b1 = -1 byte b2 = Byte.MIN_VALUE; //b2 = -128 b2 >>>= 7; //b2 = -1; NOT 1!

Does bit shift automatically promote chars to int? [duplicate]

穿精又带淫゛_ 提交于 2019-12-01 21:39:49
This question already has an answer here: Bitshift and integer promotion? 2 answers I read somewhere that bitwise shift automatically turns the operand into an int. But I'm not sure if that statement should be qualified with "if the operands are of unequal type." char one = 1, bitsInType = 8; one << (bitsInType - one); Does the default result of the second line result in an int or char? The result type is int in normal C implementations. 1 Per C 2011 (N1570) 6.5.7, “The integer promotions are performed on each of the operands. The type of the result is the that of the promoted left operand.”

Weird behaviour of bit-shifting with byte in Java

ぃ、小莉子 提交于 2019-12-01 18:55:24
As I was using bit-shifting on byte , I notice I was getting weird results when using unsigned right shift ( >>> ). With int , both right shift (signed: >> and unsigned: >>> ) behave as expected: int min1 = Integer.MIN_VALUE>>31; //min1 = -1 int min2 = Integer.MIN_VALUE>>>31; //min2 = 1 But when I do the same with byte , strange things happen with unsigned right shift: byte b1 = Byte.MIN_VALUE; //b1 = -128 b1 >>= 7; //b1 = -1 byte b2 = Byte.MIN_VALUE; //b2 = -128 b2 >>>= 7; //b2 = -1; NOT 1! b2 >>>= 8; //b2 = -1; NOT 0! I figured that it could be that the compiler is converting the byte to int

Is there a more efficient way of expanding a char to an uint64_t?

寵の児 提交于 2019-12-01 17:19:39
I want to inflate an unsigned char to an uint64_t by repeating each bit 8 times. E.g. char -> uint64_t 0x00 -> 0x00 0x01 -> 0xFF 0x02 -> 0xFF00 0x03 -> 0xFFFF 0xAA -> 0xFF00FF00FF00FF00 I currently have the following implementation, using bit shifts to test if a bit is set, to accomplish this: #include <stdint.h> #include <inttypes.h> #define BIT_SET(var, pos) ((var) & (1 << (pos))) static uint64_t inflate(unsigned char a) { uint64_t MASK = 0xFF; uint64_t result = 0; for (int i = 0; i < 8; i++) { if (BIT_SET(a, i)) result |= (MASK << (8 * i)); } return result; } However, I'm fairly new to C,

What's the purpose to bit-shift int value by zero?

别说谁变了你拦得住时间么 提交于 2019-12-01 16:26:39
Looking to the source code of java.nio.DirectByteBuffer class, I have found this: if ((length << 0) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) .... What is the purpose to shift length by zero bits? May this be some perfomance optimization or something else? I think I've solved it. In the class JavaDocs: // -- This file was mechanically generated: Do not edit! -- // So it is not hand coded. It was script-generated and the script writer did not add an optimisation for the case when the amount to bit shift by is zero. Doing i << 0 is a no-op. It evaluates to the same as i . The i << 0 is plainly

How to bitwise shift in VB.NET?

痴心易碎 提交于 2019-12-01 15:51:20
How do I bitwise shift right/left in VB.NET? Does it even have operators for this, or do I have to use some utility method? Mehrdad Afshari VB.NET has had bit shift operators ( << and >> ) since 2003. Robinicks You can use the << and >> operators, and you have to specify how many bits to shift. myFinal = myInteger << 4 ' Shift LEFT by 4 bits. myFinal = myInteger >> 4 ' Shift RIGHT by 4 bits. You can also use it as a unary operator... myFinal <<= 4 ' Shift myFinal LEFT by 4 bits, storing the result in myFinal. myFinal >>= 4 ' Shift myFinal RIGHT by 4 bits, storing the result in myFinal. 来源:

Is there a more efficient way of expanding a char to an uint64_t?

孤者浪人 提交于 2019-12-01 15:33:21
问题 I want to inflate an unsigned char to an uint64_t by repeating each bit 8 times. E.g. char -> uint64_t 0x00 -> 0x00 0x01 -> 0xFF 0x02 -> 0xFF00 0x03 -> 0xFFFF 0xAA -> 0xFF00FF00FF00FF00 I currently have the following implementation, using bit shifts to test if a bit is set, to accomplish this: #include <stdint.h> #include <inttypes.h> #define BIT_SET(var, pos) ((var) & (1 << (pos))) static uint64_t inflate(unsigned char a) { uint64_t MASK = 0xFF; uint64_t result = 0; for (int i = 0; i < 8; i+

What's the purpose to bit-shift int value by zero?

橙三吉。 提交于 2019-12-01 15:12:52
问题 Looking to the source code of java.nio.DirectByteBuffer class, I have found this: if ((length << 0) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) .... What is the purpose to shift length by zero bits? May this be some perfomance optimization or something else? 回答1: I think I've solved it. In the class JavaDocs: // -- This file was mechanically generated: Do not edit! -- // So it is not hand coded. It was script-generated and the script writer did not add an optimisation for the case when the amount to

MIPS Using Bit Shift Operators to Print a Decimal in Binary

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 14:10:06
I've read numerous threads here and elsewhere online concerning this topic. Great topics regarding bit shifts (not necessarily pertaining to Assembly but the topic in general are: What are bitwise shift (bit-shift) operators and how do they work? I've gone as far as copying and pasting the code from the OP here: How do I print a binary number with an inputed integer? and making the changes that the replier had suggested and I continue to produce a string of zero's no matter what I do. I understand what bit shifting is and how it works. Shifting to the right by 'n' divides the number by 2^n and