Ashu
Ashu

Reputation: 1733

Replacing last 4 bytes in Long number with different 4 bytes number

This is what is come up with

long longTime64Bits = 1509412598194L;
int  intTime32Bits = 63673;

longTime64Bits &= ~0xFFFFFFFF; // this should set last 32 bits to zero

long new64bitTime = longTime64Bits |= intTime32Bits; // new number with replaced 32 bits

I am not sure what is wrong but somehow longTime64Bits (line 3) new value is always coming as zero.

Upvotes: 2

Views: 200

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726539

The reason this produces incorrect result is that 0xFFFFFFFF an int constant. Therefore, ~0xFFFFFFFF is also an int, and it is equal to zero.

Changing the constant to 0xFFFFFFFF00000000L or using ~0xFFFFFFFFL will fix the problem.

Upvotes: 4

Related Questions