bit-shift

What is the most efficient way to enumerate vertices of k dimensional hypercube in C++?

痞子三分冷 提交于 2019-12-05 10:12:06
Basic Question: I have a k dimensional box. I have a vector of upper bounds and lower bounds. What is the most efficient way to enumerate the coordinates of the vertices? Background: As an example, say I have a 3 dimensional box. What is the most efficient algorithm / code to obtain: vertex[0] = ( 0, 0, 0 ) -> ( L_0, L_1, L_2 ) vertex[1] = ( 0, 0, 1 ) -> ( L_0, L_1, U_2 ) vertex[2] = ( 0, 1, 0 ) -> ( L_0, U_1, L_2 ) vertex[3] = ( 0, 1, 1 ) -> ( L_0, U_1, U_2 ) vertex[4] = ( 1, 0, 0 ) -> ( U_0, L_1, L_2 ) vertex[5] = ( 1, 0, 1 ) -> ( U_0, L_1, U_2 ) vertex[6] = ( 1, 1, 0 ) -> ( U_0, U_1, L_2 )

Java bitshift strangeness

我只是一个虾纸丫 提交于 2019-12-05 09:44:57
Java has 2 bitshift operators for right shifts: >> shifts right, and is dependant on the sign bit for the sign of the result >>> shifts right and shifts a zero into leftmost bits http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.html This seems fairly simple, so can anyone explain to me why this code, when given a value of -128 for bar, produces a value of -2 for foo: byte foo = (byte)((bar & ((byte)-64)) >>> 6); What this is meant to do is take an 8bit byte, mask of the leftmost 2 bits, and shift them into the rightmost 2 bits. Ie: initial = 0b10000000 (-128) -64 = 0b11000000

Bit shifting in Ruby

前提是你 提交于 2019-12-05 08:05:17
I'm currently converting a Visual Basic application to Ruby because we're moving it to the web. However when converting some algorithms I've run into a problem concerning bit shifting. How I understand it, the problem lies in the size mask VB enforces on Integer types (as explained Here ). Ruby, in practice, doesn't differentiate in these types. So the problem: Visual Basic Dim i As Integer = 182 WriteLine(i << 24) '-1241513984 Ruby puts 182 << 24 # 3053453312 I've been Googling and reading up on bit shifting the last hours but haven't found a way, or direction even, to tackle this problem.

Manipulating 80 bits datatype in C

烂漫一生 提交于 2019-12-05 04:12:44
I'm implementing some cryptographic algorithm in C which involves an 80 bits key. A particular operation involves a rotate shifting the key x number of bits. I've tried the long double type which if I'm not wrong is 80bits, but that doesn't work with the bitshift operator. The only alternative I can come up with is to use a 10 element char array with some complicated looping and if-else. My question is whether there's some simple and efficient way of carrying this out. Thanks. There is something a bit messed up here. If I understand you correctly, you are using a "soft" cpu on the FPGA.

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

左心房为你撑大大i 提交于 2019-12-05 02:54:23
问题 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 回答1: The code assumes that str is made of lower case letters and

Why is -1 zero fill right shift 1=2147483647 for integers in Java?

元气小坏坏 提交于 2019-12-05 00:29:59
For the program below: public class ZeroFillRightShift { public static void main(String args[]) { int x = -1; int y = x>>>1; System.out.println("x = " + x); System.out.println("y = " + y); } I get the output as follows: x = -1 y = 2147483647 The result that I got for -1>>>1 is 2147483647. If it’s the sign bit that has to be shifted, as I learned, the result should be 1073741824. Why is it 2147483647 then? The following image illustrates my problem more clearly: The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on

Fast bit shift of a byte array - CMAC subkeys

邮差的信 提交于 2019-12-04 20:59:39
问题 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

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

倖福魔咒の 提交于 2019-12-04 18:55:04
问题 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? 回答1: There are many reasons, here are some: Let's say you represent a black and white image as a sequence of

How to Increment unsigned int by 1 using bit-shifting & logical opr only?

浪尽此生 提交于 2019-12-04 17:03:55
I have a question in my assignment / project that adds 1 to an unsigned integer only using bit-shifting and logical operators . There shouldn't be any "+", "-", "*", or "/" symbols in the function. I am trying from last days but no success yet. So far I've tried the following: int A = (((B&C)<<1)^(B^C)) Can anybody help me to solve this.? You can help me in any programming language. unsigned int i = ...; unsigned int mask = 1; while (i & mask) { i &= ~mask; mask <<= 1; } i |= mask; gkuzmin Java: public static int inc(int i){ if ((i & 1) == 0) return i | 1; else return inc(i>>1)<<1; } P.S.

re implement modulo using bit shifts?

时光毁灭记忆、已成空白 提交于 2019-12-04 17:00:18
问题 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