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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 <

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

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

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 (

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

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

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

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

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 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.

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

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