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

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

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

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

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

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 (

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

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