Get broadcast ip from ip and gateway in Arduino

Is there a way to get the broadcast ip from ip and subnet in Arduino?
I know if the system has an ip of 192.168.0.2 and subnet of 255.255.255.0, then 192.168.0.255 is its broadcast ip.
But how do I get it programmatically in Arduino, without any kind of hardcoding?

I need this because, I need the Arduino's to report its presence in the network, so that its controller can detect them and control them. The controller will not be a specific machine. It could be an Android app, and whoever has the app installed should be able to identify the Arduino and control it.

Once broadcast ip is known, I can transmit periodic pings over udp, and have the controllers detect them.

If you invert the subnet and OR that with the ip address you should normally end up with the broadcast address iirc.

simply use 255.255.255.255 to broadcast in the same network. you would use network address if you want to broadcast in a specific network