FAQ Database Discussion Community


(Java) Put negative short's bits to an interger

java,bit-shift
I want to put a short integer (16 bits) into tail of a 32 bits integer (I'm compressing some small-range numbers to an unique integer). But didn't found a better looking way than bellow, I have to use two operation, can it be better? short sh = -1; // 16...

Shift operator usage in terms of classes

c++,bit-shift,bitcoin
I have this line of C++ code and don't know what the shift operator does: vRecv >> locator >> hashStop Identifiers are of the following types: vRecv: CDataStream CNetMessage::vRecv, instance of the CDataStream class and public attribute of CNetMessage class locator: CBlockLocator locator, instance of CBlockLocator struct hashStop: uint256 hashStop,...

Shift left with variable error

c++,c,bit-shift,bitmask
I'm trying to create a mask, but when I calculate the value of shift and put it inside a variable I get the wrong result. Look: #include <stdio.h> int main(){ int shift = 32 ; printf("%d\n",shift); //32 int mask = 0xFFFFFFFF << shift; printf("%x\n",mask); //ffffffff mask = 0xFFFFFFFF << 32;...

reverse function with 2 unknowns

c#,bit-shift
I'm having some difficulty reversing the following functions done before storing data to a device. ss = enum + 16 u32ts = number << 8 u32timestamp = ss+u32ts enum and number are the two vaules I'm trying to get back but I'm unawaire of what either of them are when...

Shifting Bits from a NamedPipeClientStream

c#,bit-shift
I am currently trying to write a bit synchronous serial protocol that reads data through a NamedPipeClientStream. This data then needs to be continuously monitored for sync characters (Hex 22) that may or may not be on the byte boundaries and shift the data accordingly to ensure that the other...

Shift operation in C

c,bit-manipulation,bit-shift
Is it possible to shift 0s as you can shift 1s in shifting operation in C? Something like this for (i=0; i<32; i++) { if (data & 0x01) { data |= (1<<i); } else { data &=~ (0<<i); } } I'm checking if some bits are set and depending on...

Java Shift Operator and auto promotion

java,bit-shift
I'm a newbie Java programmer, who is reading Thinking In Java by Bruce Eckel. In the 5th chapter, operators have been discussed. Where it is talking about shift operators (<<, >>, >>>) he says: If you shift a char, byte, or short, it will be promoted to int before the...

What does shift left (<<) actually do in Swift?

xcode,swift,bit-shift
I'm messing around with a Flappy Bird clone, and i can't figure out what the meaning of the following code is let birdCategory: UInt32 = 1 << 0 let worldCategory: UInt32 = 1 << 1 let pipeCategory: UInt32 = 1 << 2 let scoreCategory: UInt32 = 1 << 3 Sorry...

segmentation fault while trying to manipulate bits

c,pointers,bit-shift,bits
I am studying through a book called Computer Systems: A Programmer's Perspective to gain some insight about the inner workings of modern computers. Tonight I was trying to look inside int's and unsigned's to see of what bits they are made. I came up with this: #include <stdbool.h> #include <stdio.h>...

How does this reverseBytes method work?

c,byte,bit,bit-shift
I was looking at this function online and am wondering how it works: /* * reverseBytes - reverse bytes * Example: reverseBytes(0x12345678) = 0x78563412 * Legal ops: ! ~ & ^ | + << >> */ int reverseBytes(int x) { int newbyte0 = (x >> 24) & 0xff; int newbyte1...

Why is 1 << 3 equal to 8 and not 6? [duplicate]

c,bit-shift
This question already has an answer here: What are bitwise shift (bit-shift) operators and how do they work? 5 answers In C I have this enum: enum { STAR_NONE = 1 << 0, // 1 STAR_ONE = 1 << 1, // 2 STAR_TWO = 1 << 2, // 4...

Strength reduction techniques with Bison

optimization,bison,bit-shift
I have a Bison parser generator and I'm looking for ways to optimize it. Would techniques such as bit shifting for multiplication and division, or creating checks for x*2 and changing it to x+x make it faster? Or is bison already optimized for this sort of thing.

Delphi XE LiveBindings - Bits to Byte

delphi,bit-shift,delphi-xe7,livebindings
I just discovered livebindings with Delphi. And created my first components for handling a control-word for a frequency converter. The component it self seems to work well testing it in the form designer. However, compiling and running the application things doesn't work. Screenshot from livbindings like this: And here is...

How do you do an unsigned/logical shift right on an Integer in Haskell?

haskell,bit-shift
shiftR in Data.Bits does an arithmetic shift for Integers. Is there a standard way to do a logical shift right? I can think of two round about ways to do this. Take the quotient by 2^n: unsignedShiftR i n = i `quot` (2 ^ n) Another way would be to...

circular bit shifting in byte array in java

java,bytearray,bit-shift
How to shift one byte(8 bit shifting) to the left for 16 byte array.? i.e. Array of 16 bytes: (0x) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F after shifting 8 bits left: output should be: (0x) 01 02 03 04 05...

Is it possible to reverse a bitshift and addition

java,reverse-engineering,bit-shift
is it possible to reverse this in java? byte config = (byte)((X << 6) + Y); knowing that X is 0,1,2 or 3 (2 bits) and Y is between 0 and 24 (5 bits)...

Bitwise operation bit check

c,bit-manipulation,bitwise-operators,bit-shift
Is this correct way to check if bits are set in some variable and store them in another variable? Just ignore variable names, image[] and mask variables are 1 byte and first_hline_first_row and second_hline_first_row are 2 byte. for(i=0; i<16; i++){ if(image[i] & mask1) first_hline_first_row |= (1<<i); else first_hline_first_row &=~ (0<<i);...

Mysteries of C++ optimization

c++,optimization,bit-shift,multiplication
Take the two following snippets: int main() { unsigned long int start = utime(); __int128_t n = 128; for(__int128_t i=1; i<1000000000; i++) n = (n * i); unsigned long int end = utime(); cout<<(unsigned long int) n<<endl; cout<<end - start<<endl; } and int main() { unsigned long int start =...

Issue with uint64_t type

c,bitwise-operators,avr,bit-shift,unsigned-integer
say I have the the following union : typedef union { char array[8]; uint64_t u64; } my_type ; I want to shift one bit 1 through all the 64 bits the reserved, here is what I've tried : ........................... my_type vector ={0}; short index =0; for ( index=0 ; index...

Bitmask and -offset by offset and length

c#,.net,bit-manipulation,bit-shift
I want to create a bitmask and the bitmask offset by offset and length. For example: offset = 8 and length = 4 the bitmask should be: 0xF00 = 3840 in decimal(1111.0000.0000) and the bitoffset should be: 0x100 = 256 in decimal I've tried the following, but that's wrong: ulong...

Bitwise (Bitshift) operations on 64-bit integers in C++

c++,bit-manipulation,bit-shift,bitwise-or,bitboard
I'm trying to deal with bitboards, which requires me to set a particular bit in a 64-bit unsigned integer. To set bit i, I perform a bitwise OR with the bitboard in question, with a left shifted number. #include <stdint.h> uint64_t kings = 0ULL; // Also tried unsigned long long...

How to implement bit-shift in freemarker?

freemarker,bit-shift
I'm working on captcha, but the server doesn't support php and i can't add java libraries to it. So I have to implement check on freemarker, but the function using binary shift <<, is it any way to implement it in freemarker?

Is it possible to micro-optimize “x = max(a,b); y = min(a,b);”?

c++,algorithm,c++11,optimization,bit-shift
I had an algorithm that started out like int sumLargest2 ( int * arr, size_t n ) { int largest(max(arr[0], arr[1])), secondLargest(min(arr[0],arr[1])); // ... and I realized that the first is probably not optimal because calling max and then min is repetitious when you consider that the information required to...

Counting bits and printing result LC3 Assembly

assembly,output,bit-shift,bits,lc3
I'm trying to write a simple program that takes a 16-bit integer from Data1, counts how many bits are 1-bits, and then prints the result to the screen. I'm quite certain my code should do the trick, but for some reason the output I get is a little box symbol,...

How many number of primitive operations does a 16, 32 or a 64-bit processor execute to perform logical right shift of an N-bit Binary number? [closed]

algorithm,cryptography,bit-shift,polynomial-math,ecdsa
Recently,I have been trying to understand how the Binary Extended Euclidean Algorithm works at the processor level. This question is all about finding an Inverse element in GF(2^m) with polynomial basis. Generally I came across the Extended Euclidean Algorithm for evaluating an inverse element but the fact is that it...

Fast bit shift of a byte array - CMAC subkeys

performance,cryptography,bit-manipulation,bit-shift,javacard
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) |...

Bit Shifting leaving out zeros

bit-manipulation,bit-shift
I have this: int line = 0; line |= (1 << 0); line |= (1 << 1); line |= 0000000000; I was expecting an output of 000000011. But the output is 11. Why is my result truncated? I am using Java. So System.out.print();...

GLSL - compressing/packing multiple 0-1 colours (var4) into a single var4 variable

compression,glsl,bit-shift,glsles,bit-packing
I am trying to do the following in GLSL 2 es: Given a number (say 4 for example) of normalized var4 variables(RGBA) reduce their bit depth and pack the results in a single 0-1 clamped var4. This will be stored as an 8 bit (per channel) texture and later unpacked....

Store 2 values in one variable

go,binary,bit-shift,bits
Is this Go code correct and portable, I need to store 2 counters (each call only one counter will be updated) in one variable to avoid locks in actual code where I am going to use single atomic.AddUint64() instead of locking whole struct. package main import "fmt" var long uint64...

Why does shifting this bit evalue to 51

c++,bitwise-operators,bit-shift
I'm currently learning for a C++ examn. One of the questions in the practice examn is: What is the output of this statement. cout <<(11>>1)<<1<<endl; As I see it. 11 holds the binary equivalent of 1011. Shifting this binary number with 1 bit to the right makes it: 0101 Then...

Hex *char from int number

c++,hex,bit-shift
I asked here how to convert 4 chars to int number here: stackoverflow link It is working but now I need to do the same but in reverse. I want to make 4 chars from one int. Because I dont understand completly bit shifting and using OR and AND operators...

How to bit-shift and concatenate to get correct result?

c#,concatenation,bit,bit-shift,uint16
I'm currently struggling with modbus tcp and ran into a problem with interpreting the response of a module. The response contains two values that are encoded in the bits of an array of three UInt16 values, where the first 8 bits of r[0] have to be ignored. Let's say the...

How to convert 8bit sound to 16bit

c#,vb.net,bit-shift,monogame
I'm using DynamicSoundEffectInstance to stream in raw PCM data. This data is 16bit and signed. My code works fine, not a single problem. The issue is compressing this raw PCM yields poor results: 13.9mb down to 13.7mb. If I encode the PCM data as 8bit signed values, and then compress...

constructing key by bit shifting 3 integers in C

c,key,bit-shift
I want to construct a key composed of 3 values by using bit shifting operations: According to my understanding, the C statement code I am starting from creates a hash table by constructing its keys from certain data variables: uint64_t key = (uint64_t)c->pos<<32 | c->isize; My interpretation is that key...