Operation of servo map faction

long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

val = map(val, 0, 1023, 0, 255);

It should be much appreciated, if some one can explain, how above code is working in side the mega 2560 micro controller

I mean operation of val = map(val, 0, 1023, 0, 255); code

I am not asking definition of above function

Thanks in advance

Say you were mapping a reading of 128 from the 20-1023 range into the 10-255 range, just to have 5 different #s to show.
Then the output would be:
long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (128 - 20) * (255 - 10) / (1023 - 10) + 10;
}
Or (108 * 245)/1013 + 10 = 36

This case:
val = map(val, 0, 1023, 0, 255);
is really just output = input/4
look at in binary: 11111111xx ->11111111
10 bits to 8 bits is right shift 2, the xx bits get dropped off. Each right shift by 1 is same as divide by 2, 2 shifts = divide by 4.

val = map(val, 0, 1023, 0, 255);Is a little skewed..
val = map(val, 0, 1024, 0, 256);Is more natural.
val /= 4;Is simpler and quicker.

I don't know about skewed. 0-1023 is what comes out of analogRead, 0-255 is sent to analogWrite.
1024 and 256 both need an extra bit.

This case:
val = map(val, 0, 1023, 0, 255);
is really just output = input/4

Nope.

void setup() 
{
  Serial.begin (115200);
  Serial.println (F("Brace yourself..."));
  int fails = 0;
  for (int i = 0; i < 1024; i++)
  {
    int x = (i / 4);
    int y = map (i, 0, 1023, 0, 255);
    if (x != y)
    {
      oops (x, y);
      fails++;
    }
  }
  fail (fails);
  
  Serial.println (F("And now, much quieter..."));
  fails = 0;
  for (int i = 0; i < 1024; i++)
  {
    int x = (i / 4);
    int y = map (i, 0, 1024, 0, 256);
    if (x != y)
    {
      oops (x, y);
      fails ++;
    }
  }
  fail (fails);
}  

void oops (int x, int y)
{
  Serial.print (x);
  Serial.print (F(" != "));   
  Serial.println (y);
}

void fail (int fails)
{
  Serial.print (F("Got it \"wrong\" "));
  Serial.print (fails);
  Serial.println (F(" times"));
}

void loop() {}