Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there binary way to convert integer hundred position to zero?

Tags:

c#

I'm trying to make a function HundredPosToZero to convert hundred position to zero, for example :

HundredPosToZero(4239) // 4039

This is my implement :

public HundredPosToZero(int num){
  return num / 1000 * 1000 + num % 100;
}

However, I'm thinking of why not use bitwise operator like 4239 & 1011 to do the same thing? But I can not figure out how to implement it since 4239 is not a binary, any advice with this approach?

like image 212
Sing Avatar asked Dec 06 '25 04:12

Sing


2 Answers

Not really. There is nothing special about decimal hundreds in binary. It would be possible with e.g. a hexadecimal number, but decimal numbers don't play well with binary :)

like image 120
Luaan Avatar answered Dec 08 '25 16:12

Luaan


It is simply impossible if you want this operation to be the same for all numbers as "hundreds" in regular representation of integers don't occupy the same set of bits.

If you use some other binary representation of numbers (like BCD) that allocates groups of bits to unique decimal digits then you can do that easily.

like image 28
Alexei Levenkov Avatar answered Dec 08 '25 17:12

Alexei Levenkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!