## Combination of 2 uint16_t in 1 uint32_t

I have 2 uint16_t's that i want to combine into 1 32 bit number: uint16_t var1 = 255; // 0000 0000 1111 1111 uint16_t var2 = 255; // 0000 0000 1111 1111 uint32_t var3 = (var1 << 16) + var2; I expect var3 to be 0000 0000 1111 1111 0000 0000 1111 1111

## Do Delphi and FPC have MakeWord functions?

These functions are useful: function MakeWord(low, high: char): word; begin result := byte(high) shl 8 + byte(low); end; function MakeWord(low, high: byte): word; begin result := high shl 8 + low; end; However I am wondering if these kinds of functio

## Bitshift to 64th bit in Java

For a programm, i need to shift the bits from the first to the last bit. So i would need to: long a = 0; a |= 1L<<64; Since the long isn't unsigned, it doesn't work. Is there a possibility to shift a 1 to this last Bit? I'm taking a long and not a B

## Combine 2 bytes in a short circuit in C

I am receiving a short int via network packet, which means it would come as 2 bytes in network byte order (big endian). I would like to combine the two bytes I received into a short int variable on my machine, which is little endian byte order. Examp

## VS compilation alert: result of 32-bit offset implicitly converted to 64-bit

Visual Studio 2013 issues an annoying (and seemingly irrelevant) compilation warning on: #include <stdint.h> #define PRECISION 16 uint64_t hi = 0; for (uint8_t i = 0; i < PRECISION; i++) { if (some_condition) { hi += 1 << (PRECISION - 1 - i

## Very fast way to check the C setting bit

I'm using some sort of BitStream in my code that has a read_bit()-function. This function is called very very often (more than one billion times in a single stream). This is what the struct BitStream looks like: typedef struct BitStream { unsigned ch

## Right arithmetic of the signed integer

The C99 spec states: The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 / 2^E2. If E1 has a s

## How to randomize the lowest bits in a 64-bit model

I am interested in learning more about the concepts behind bit shifting and the manipulation of randomly generated bits. Below is some code that prints 20 random 64 bit patterns. "rand_bits" uses the C standard rand() to return a 64 bit pattern

## The time complexity of a loop that integer divides the loop counter by a constant

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 } I

## How do you make an unsigned / logical shift on an integer in Haskell?

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 ma

## Invalid operation: float64 offset

I'm facing a strange issue using the shift operator << in Golang. In my final code, the shift value will be the absolute value of two integers. However, the Go package only defines the Abs function for float64 values, so I will need to cast the para

## Left shift operator

If I have the following: char v = 32; // 0010 0000 then I do: v << 2 the number becames negative. // 1000 0000 -128 I read the standard but it is only written: If E1 has a signed type and nonnegative value, and E1 × 2 E2 is representable in the resu

## Why combine two changes of one uint8_t produces a different result?

Could someone explain me why: x = x << 1; x = x >> 1; and: x = (x << 1) >> 1; produce different answers in C? x is a *uint8_t* type (unsigned 1-byte long integer). For example when I pass it 128 (10000000) in the first case it retu

## Define a specific bit in the byte array

I want to know how to set a specific bit in a 16 byte array (128 bits). For example ... if I wanted to set the 9th bit in the the array I would expect: {00, 80, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00} If I wanted to set the 125th bit

## Hiding bits vs. moving (+ java bytecode)

When working with IEEE754 floats I've found two ways of zeroing top 9 bits of a 32bit variable. Is any of them better than the other? Java related addition: is there some list providing information about instruction efficiency? I've randomly found ou

## Get the integer from bits inside `std :: vector & lt; char & gt;

I have a vector<char> and I want to be able to get an unsigned integer from a range of bits within the vector. E.g. And I can't seem to be able to write the correct operations to get the desired output. My intended algorithm goes like this: & th

## Find the most significant bit in a long

I'm in the unique situation where searching for "most significant bit" yields too many results and I can't find an answer that fits my needs! The question itself is pretty simple: "How do I find the most significant set bit in an unsigned l

## find the bit positions in a byte

I was tyring to create a sparse octree implementation like the people at nVidia ("Efficient Sparse Voxel Octrees") were doing for their voxel things when I came across this issue: I have a bitfield of type byte (so 8 bits only) that tells me whe

## Bit transfer and assignment

This question already has an answer here: Why doesn't left bit-shift, "<<", for 32-bit integers work as expected when used more than 32 times? 9 answers This is sort of driving me crazy. int a = 0xffffffff; int b = 32; cout << (a <

## Shift Right Logic of only ADD and NAND?

I'm making a multiplier in a very simple assembly language in which I have BEQ, NAND, and ADD to create a SRL. I also have to keep the multiplier under 50 lines (16 used thus far) so hopefully the solution can be thrown in a loop. EDIT: My question i

## Bit changing for fixed-point arithmetic on floating-point C numbers

i wrote the following test code to check fixed point arithmetic and bit shifting. void main(){ float x = 2; float y = 3; float z = 1; unsigned int * px = (unsigned int *) (& x); unsigned int * py = (unsigned int *) (& y); unsigned int * pz = (unsi

## Right shift operator in C?

I got the following code: int main(int argc, char *argv[]) { char c = 128; c = c >> 1; printf("c = %d\n", c); return 0; } Run the above code on win xp 32 bit, I got the result: -64. Why -64?Because the char type is a signed 8-bit integer (

## Curiosity: why post operators have less priority than the additive?

I'm wondering why the shift operators (<< and >>), being equivalent to a multiplication and a division respectively, do have less priority than an additive operator, such the "+". In other words: int a = 1 + 2 * 8; //yields 17 wherea

## Why not left bit-shift, & lt; & lt; & lt; & rt;, for 32-bit integers work as expected when used more than 32 times?

When I write the following program and use the GNU C++ compiler, the output is 1 which I think is due to the rotation operation performed by the compiler. #include <iostream> int main() { int a = 1; std::cout << (a << 32) << std::e

## Scala BitSet and offset operations

I'm looking for a way to represent a set of integers with a bit vector (which would be the characteristic function of that set of integers) and be able to perform bitwise operations on this set. Initially I thought scala's BitSet would be the ideal c

## Problem with bitwise cylinder offset Rotate left and right in C #

In C++ I have code like this. static UInt32 rol(UInt32 value, UInt32 bits) { bits &= 31; return ((value << bits) | (value >> (32 - bits))); } static UInt32 ror(UInt32 value, UInt32 bits) { bits &= 31; return ((value >> bits) | (v

## bit shift different result in similar programs

Here is one program #include<stdio.h> #include<stdlib.h> int main() { unsigned char a=0x80; printf("%d\n",a<<1); } The output of above is 256 Now here is one more version of above program #include<stdio.h> #include<std

## How to change bitwise in VB.NET?

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?VB.NET has had bit shift operators (<< and >>) since 2003.

## Java: Check if a bit is 0 or 1 in a long

What method would you use to determine if the the bit that represents 2^x is a 1 or 0 ?I'd use: if ((value & (1L << x)) != 0) { // The bit was set } (You may be able to get away with fewer brackets, but I never remember the precedence of bitwise

## What are bit-shift operators and how do they work?

I've been attempting to learn C in my spare time, and other languages (C#, Java, etc.) have the same concept (and often the same operators) ... What I'm wondering is, at a core level, what does bit-shifting (<<, >>, >>>) do, what prob