1

I want to convert a number to 32 Bit binary data. When I try to convert a number to 16 bit or 8 bit data, there is no any problem. But When I try 32 Bit, result is not correct.

8 Bit example:

-1 & 255 //result -> 255 or -1 & 0xFF //result -> 255

16 Bit example:

-1 & 65535 //result -> 65535 or -1 & 0xFFFF //result -> 65535

Wrong result in 32 Bit

-1 & 4294967295 //result -> -1 or -1 & 0xFFFFFFFF //result -> 4294967295

My expectation in 32 bit, result is 4294967295. -1 is not a correct answer.

How can solve my problem. Thanks

SefaUn
  • 824
  • 1
  • 7
  • 23
  • Two's compliment? https://en.wikipedia.org/wiki/Two%27s_complement Possible answers? https://stackoverflow.com/questions/31246843/why-does-4294967295-the-highest-number-at-32bit-equal-1 or https://stackoverflow.com/questions/1863153/why-unsigned-int-0xffffffff-is-equal-to-int-1 – Robin Webb Jun 09 '22 at 07:37
  • I already do that. but it does not work for 32 bit data :) @RobinWebb – SefaUn Jun 09 '22 at 07:47

1 Answers1

0

I found answer of my question.

this is not working for 32 bit

-1 & 4294967295

this is working for 32 bit

-1 >>> 0 //result -> 4294967295

but I don't understand the reason of this error.

SefaUn
  • 824
  • 1
  • 7
  • 23