Help me in solving BITMEDU4 problem

My issue

finding lsb,msb,then fliping !is my code correct or not
l=n&1;
m=Integer.highestOneBit(n);//or m=n>>1;
f=n^(1<<l);
k=n^(1<<m);
System.out.println(k)

My code

import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int l,m,f,k,v;
		l=n&1;
		m=Integer.highestOneBit(n);
		f=n^(1<<l);
		k=n^(1<<m);
		System.out.println(k);

	}
}

Learning course: Bit manipulation
Problem Link: Least significant bit, and most significant bit Practice Problem in Bit manipulation - CodeChef

@ace0613
plzz refer the following c++ code

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int n;
	cin>>n;
	// fliping the least significant bit
	n = n^1;
	// finding the position of most significant bit 
	int pos = 0;
	int n1 = n;
	while(n1!=0)
	{
	    n1 /=2;
	    pos++;
	}
	pos--;
	// fliping the most significant bit
	n = n^(1<<pos);
	cout<<n<<"\n";
	
	return 0;
}