bit-shift

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

荒凉一梦 提交于 2019-12-04 02:13:57
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 However this doesn't seem right as (I think) it turns all numbers negatives? EDIT2: Several hours

Unsigned integer bit field shift yields signed integer

*爱你&永不变心* 提交于 2019-12-04 01:10:59
Let consider the following program test.c : #include <stdio.h> struct test { unsigned int a:5; }; int main () { unsigned int i; struct test t = {1}; for (i = 0; i < t.a << 1; i++) printf("%u\n", i); return 0; } When compiled with gcc -Wsign-compare test.c the following warning is produced (tested with gcc 4.8.1): test.c:9:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for (i = 0; i < t.a << 1; i++) ^ clang -Wsign-compare test.c produces the following (tested with clang 3.2): test.c:9:19: warning: comparison of integers of different signs: 'unsigned int

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

故事扮演 提交于 2019-12-03 22:29:50
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, please explain why I must add "i64" to the end, and why this will not affect a 32-bit compilation.

unique chars with shift and operators : don't understand this code

瘦欲@ 提交于 2019-12-03 20:13:32
i don't understand the lines in the loop : we take the character and subtract a , so "10" ? (why?) then 1 << val : we shift 1 by val ? (why?) and checker is 0, so how do we reach > 0 in the condition? public static boolean isUniqueChars(String str) { int checker = 0; for (int i = 0; i < str.length(); i++) { int val = str.charAt(i) - 'a'; if ((checker & (1 << val)) > 0) return false; checker |= (1 << val); } return true; } Thanks The code assumes that str is made of lower case letters and returns true if there are no repeating letters. It works like this: checker is used as a bitmap, that is,

Fast bit shift of a byte array - CMAC subkeys

断了今生、忘了曾经 提交于 2019-12-03 13:13:37
I need to implement as fast as possible left bit shift of a 16-byte array in JavaCard . I tried this code: private static final void rotateLeft(final byte[] output, final byte[] input) { short carry = 0; short i = (short) 16; do { --i; carry = (short)((input[i] << 1) | carry); output[i] = (byte)carry; carry = (short)((carry >> 8) & 1); } while (i > 0); } Any ideas how to improve the performace? I was thinking about some Util.getShort(...) and Util.setShort(...) magic, but I did not manage to make it work faster then the implementation above. This is one part of CMAC subkeys computation and it

Are there any good reasons to use bit shifting except for quick math?

↘锁芯ラ 提交于 2019-12-03 12:40:50
I understand bitwise operations and how they might be useful for different purposes, e.g. permissions. However, I don't seem to understand what use the bit shift operators are. I understand how they work, but I can't think of any scenarios where I might want to use them unless I want to do some really quick multiplication or division. Are there any other reasons to use bit-shifting? Guy Sirton There are many reasons, here are some: Let's say you represent a black and white image as a sequence of bits and you want to set a single pixel in this image generically. For example your byte offset may

Will bit-shift by zero bits work correctly?

那年仲夏 提交于 2019-12-03 11:29:32
问题 Say I have a function like this: inline int shift( int what, int bitCount ) { return what >> bitCount; } It will be called from different sites each time bitCount will be non-negative and within the number of bits in int . I'm particularly concerned about call with bitCount equal to zero - will it work correctly then? Also is there a chance that a compiler seeing the whole code of the function when compiling its call site will reduce calls with bitCount equal to zero to a no-op? 回答1: It is

re implement modulo using bit shifts?

夙愿已清 提交于 2019-12-03 10:48:46
I'm writing some code for a very limited system where the mod operator is very slow. In my code a modulo needs to be used about 180 times per second and I figured that removing it as much as possible would significantly increase the speed of my code, as of now one cycle of my mainloop does not run in 1/60 of a second as it should. I was wondering if it was possible to re-implement the modulo using only bit shifts like is possible with multiplication and division. So here is my code so far in c++ (if i can perform a modulo using assembly it would be even better). How can I remove the modulo

Why do we need to use shift operators in java?

扶醉桌前 提交于 2019-12-03 10:39:10
What is the purpose of using Shift operators rather than using division and multiplication? Are there any other benefits of using shift operators? Where should one try to use the shift operator? Division and multiplication are not really a use of bit-shift operators. They're an outdated 'optimization' some like to apply. They are bit operations, and completely necessary when working at the level of bits within an integer value. For example, say I have two bytes that are the high-order and low-order bytes of a two-byte (16-bit) unsigned value. Say you need to construct that value. In Java, that

n is negative, positive or zero? return 1, 2, or 4

此生再无相见时 提交于 2019-12-03 10:23:13
I'm building a PowerPC interpreter, and it works quite well. In the Power architecture the condition register CR0 (EFLAGS on x86) is updated on almost any instruction. It is set like this. The value of CR0 is 1, if the last result was negative, 2 if the last result was positive, 4 otherwise. My first naive method to interpret this is: if (n < 0) cr0 = 1 else if (n > 0) cr0 = 2; else cr0 = 4; However I understand that all those branches won't be optimal, being run millions of times per second. I've seen some bit hacking on SO, but none seemed adeguate. For example I found many examples to