bit-shift

SHR and SAR Commands

北城以北 提交于 2020-01-11 14:24:06
问题 I would like to make sure I am understanding this concept 100% and if not get some clarification. In an asm program, if you perform SHR 00110000b you would end up with 00011000b . However, if you were to perform SHR on 11111111b you would end up with an incorrect answer and should use SAR instead? This is because the number is signed? 回答1: if you perform SHR 00110000b you would end up with 00011000b If you shifted one bit to the right, yes. You can specify the shift amount, so it's not fixed

Time Complexity of a loop that integer divides the loop counter by a constant

吃可爱长大的小学妹 提交于 2020-01-10 05:33:04
问题 I'm trying to calculate the time complexity of a simple algorithm in big O notation, but one part of it is seriously boggling my mind. Here is a simplified version of the algorithm: int a=n while(a>0) { //for loop with time complexity n^3 a = a/8 } In this instance, it's integer division, so the while loop will terminate after a's value drops below 8. I'm not sure how to express this in terms of n. I'd also like to know how to tackle future calculations like this, where the number of loops

Shift N bits an entire array of chars

倖福魔咒の 提交于 2020-01-07 05:38:26
问题 Let's say I have an array of chars and I want to shift N bits to the left each byte, carrying to the left, so only the N bits of the first character would be lost. Example: kxmo shifted 3 bits to the left should become X@hx This is what I have currently, but it's not working as expected: #include <stdio.h> int main(void) { //shift the array with length *len* *shift* bits to the left int len = 4, shift = 3; unsigned char a[len] = "kxmo"; unsigned char b[len]; //X@hx unsigned char tmp = 0, tmp2

vb.net code for Android app

↘锁芯ラ 提交于 2020-01-06 08:23:08
问题 I am working on a BlackBerry app. This app has already been developed in Android in "Basic4Android" program. The code for the android app is written in vb.net. I need to write the exact same code for BlackBerry. I am trying to understand this method but I am unable to since I am not an expert with vb.net. I will not paste the entire method but only parts which I need to clarify: Sub HEX_T24_SHORT_GPS(latitude_decimal_degrees As Double, longitude_decimal_degrees As Double, speed_knots As Int,

Java Bit Operations: replacing nibble of hex [duplicate]

谁说胖子不能爱 提交于 2020-01-06 08:10:34
问题 This question already has answers here : set 4-bit nibble in an int type (3 answers) Closed 3 years ago . I have to write this code for a homework assignment but I don't even know where to start. Here is the javadoc to the method I have to write. /** * Sets a 4-bit nibble in an int * Ints are made of eight bytes, numbered like so: 7777 6666 5555 4444 3333 2222 1111 0000 * * For a graphical representation of this: * 1 1 1 1 1 1 * 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

Multiplication using Logical shifts in MIPS assembly

早过忘川 提交于 2020-01-02 18:03:38
问题 Can someone please give me pointers on how I can go about making a code that multiplies using shifts in MIPS assembly? I don't understand how having a number 2^n can help me multiply using an odd multiplicand I currently have this code, I'm trying to make a calculator .text li $v0, 4 la $a0, ask_1 syscall li $v0,5 syscall move $s1, $v0 li $v0, 4 la $a0, ask_2 syscall li $v0,5 syscall move $s2, $v0 #sll $s2, $s2, 3 #$s2 * $s2^3 = result srl $s2, $s2, 1 li $v0, 1 la $a0, ($s2) syscall .data ask

How do I save multiple small integers in one integer via bitshifting?

南楼画角 提交于 2020-01-02 16:59:21
问题 I am working in an int[][][] array and I need to return an address of one field of that array from a static function. Given the fact that the dimensions of the Array will stay small ( int[32][32][32] ) I had the idea to return one number containing all three Values, instead of using an Array containing the three numbers. I already had a working solution, where i packed my number into a String and unpacked it in the receiving method via Integer.parseInt(String) . Unfortunately this didn't work

Why does bit-wise shift left return different results in Python and Java?

随声附和 提交于 2020-01-01 08:24:15
问题 I'm trying to port some functionality from a Java app to Python. In Java, System.out.println(155 << 24); Returns: -1694498816 In Python: print(155 << 24) Returns 2600468480 Many other bitwise operations have worked in the same way in both languages. Why is there a different result in these two operations? EDIT: I'm trying to create a function in python to replicate how the left shift operator works in Java. Something along the lines of: def lshift(val, n): return (int(val) << n) - 0x100000000

Why do shift operations always result in a signed int when operand is <32 bits

早过忘川 提交于 2020-01-01 08:18:39
问题 Why do shift operations on unsigned ints give an unsigned result, but operations on smaller unsigned operands result in a signed int? int signedInt = 1; int shiftedSignedInt = signedInt << 2; uint unsignedInt = 1; uint shiftedUnsignedInt = unsignedInt << 2; //OK. unsigned result short signedShort = 1; int shiftedsignedShort = signedShort << 2; ushort unsignedShort = 1; uint shiftedUnsignedShort = unsignedShort << 2; //CS0266: Can't cast int to uint sbyte signedByte = 1; int shiftedSignedByte

In C++, what is the difference between 1 and 1i64?

倖福魔咒の 提交于 2020-01-01 07:33:06
问题 I'm converting some 32-bit compatible code into 64-bit - and I've hit a snag. I'm compiling a VS2008 x64 project, and I receive this warning: warning C4334: '<<' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?) Here's the original line of code: if ((j & (1 << k)) != 0) { And here's what it looks like if I follow Microsoft's advice: if ((j & (1i64 << k)) != 0) { Is this safe to do, when the code will be compiled on both 32-bit and 64-bit systems? If so,