> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/the-sql-language/functions/functions-net.md).

# 9.12. Network Address Functions and Operators \#

The IP network address types, `cidr` and `inet`, support the usual comparison operators shown in [Table 9.1](/the-sql-language/functions/functions-comparison.md#FUNCTIONS-COMPARISON-OP-TABLE) as well as the specialized operators and functions shown in [Table 9.39](#CIDR-INET-OPERATORS-TABLE) and [Table 9.40](#CIDR-INET-FUNCTIONS-TABLE).

Any `cidr` value can be cast to `inet` implicitly; therefore, the operators and functions shown below as operating on `inet` also work on `cidr` values. (Where there are separate functions for `inet` and `cidr`, it is because the behavior should be different for the two cases.) Also, it is permitted to cast an `inet` value to `cidr`. When this is done, any bits to the right of the netmask are silently zeroed to create a valid `cidr` value.

**Table 9.39. IP Address Operators**

| <p>Operator</p><p>Description</p><p>Example(s)</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |                                                                                                         |                                                          |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------- | -------------------------------------------------------- |
| <p><code>inet</code> <code><<</code> <code>inet</code> → <code>boolean</code></p><p>Is subnet strictly contained by subnet? This operator, and the next four, test for subnet inclusion. They consider only the network parts of the two addresses (ignoring any bits to the right of the netmasks) and determine whether one network is identical to or a subnet of the other.</p><p><code>inet '192.168.1.5' << inet '192.168.1/24'</code> → <code>t</code></p><p><code>inet '192.168.0.5' << inet '192.168.1/24'</code> → <code>f</code></p><p><code>inet '192.168.1/24' << inet '192.168.1/24'</code> → <code>f</code></p> |                                                                                                         |                                                          |
| <p><code>inet</code> <code><<=</code> <code>inet</code> → <code>boolean</code></p><p>Is subnet contained by or equal to subnet?</p><p><code>inet '192.168.1/24' <<= inet '192.168.1/24'</code> → <code>t</code></p>                                                                                                                                                                                                                                                                                                                                                                                                            |                                                                                                         |                                                          |
| <p><code>inet</code> <code>>></code> <code>inet</code> → <code>boolean</code></p><p>Does subnet strictly contain subnet?</p><p><code>inet '192.168.1/24' >> inet '192.168.1.5'</code> → <code>t</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                     |                                                                                                         |                                                          |
| <p><code>inet</code> <code>>>=</code> <code>inet</code> → <code>boolean</code></p><p>Does subnet contain or equal subnet?</p><p><code>inet '192.168.1/24' >>= inet '192.168.1/24'</code> → <code>t</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                  |                                                                                                         |                                                          |
| <p><code>inet</code> <code>&&</code> <code>inet</code> → <code>boolean</code></p><p>Does either subnet contain or equal the other?</p><p><code>inet '192.168.1/24' && inet '192.168.1.80/28'</code> → <code>t</code></p><p><code>inet '192.168.1/24' && inet '192.168.2.0/28'</code> → <code>f</code></p>                                                                                                                                                                                                                                                                                                                      |                                                                                                         |                                                          |
| <p><code>~~</code> <code>inet</code> → <code>inet</code></p><p>Computes bitwise NOT.</p><p><code>~~ inet '192.168.1.6'</code> → <code>63.87.254.249</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |                                                                                                         |                                                          |
| <p><code>inet</code> <code>&</code> <code>inet</code> → <code>inet</code></p><p>Computes bitwise AND.</p><p><code>inet '192.168.1.6' & inet '0.0.0.255'</code> → <code>0.0.0.6</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                      |                                                                                                         |                                                          |
| <p><code>inet</code> <code>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    | </code> <code>inet</code> → <code>inet</code></p><p>Computes bitwise OR.</p><p><code>inet '192.168.1.6' | inet '0.0.0.255'</code> → <code>192.168.1.255</code></p> |
| <p><code>inet</code> <code>+</code> <code>bigint</code> → <code>inet</code></p><p>Adds an offset to an address.</p><p><code>inet '192.168.1.6' + 25</code> → <code>192.168.1.31</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                     |                                                                                                         |                                                          |
| <p><code>bigint</code> <code>+</code> <code>inet</code> → <code>inet</code></p><p>Adds an offset to an address.</p><p><code>200 + inet '::ffff:fff0:1'</code> → <code>::ffff:255.240.0.201</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                          |                                                                                                         |                                                          |
| <p><code>inet</code> <code>-</code> <code>bigint</code> → <code>inet</code></p><p>Subtracts an offset from an address.</p><p><code>inet '192.168.1.43' - 36</code> → <code>192.168.1.7</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                              |                                                                                                         |                                                          |
| <p><code>inet</code> <code>-</code> <code>inet</code> → <code>bigint</code></p><p>Computes the difference of two addresses.</p><p><code>inet '192.168.1.43' - inet '192.168.1.19'</code> → <code>24</code></p><p><code>inet '::1' - inet '::ffff:1'</code> → <code>-4294901760</code></p>                                                                                                                                                                                                                                                                                                                                      |                                                                                                         |                                                          |

<br>

**Table 9.40. IP Address Functions**

| <p>Function</p><p>Description</p><p>Example(s)</p>                                                                                                                                                                                                                                                                                                                                                                                            |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>abbrev</code> ( <code>inet</code> ) → <code>text</code></p><p>Creates an abbreviated display format as text. (The result is the same as the <code>inet</code> output function produces; it is “abbreviated” only in comparison to the result of an explicit cast to <code>text</code>, which for historical reasons will never suppress the netmask part.)</p><p><code>abbrev(inet '10.1.0.0/32')</code> → <code>10.1.0.0</code></p> |
| <p><code>abbrev</code> ( <code>cidr</code> ) → <code>text</code></p><p>Creates an abbreviated display format as text. (The abbreviation consists of dropping all-zero octets to the right of the netmask; more examples are in <a href="/pages/eVDt7gFlIR9HgiJ2ojys#DATATYPE-NET-CIDR-TABLE">Table 8.22</a>.)</p><p><code>abbrev(cidr '10.1.0.0/16')</code> → <code>10.1/16</code></p>                                                        |
| <p><code>broadcast</code> ( <code>inet</code> ) → <code>inet</code></p><p>Computes the broadcast address for the address's network.</p><p><code>broadcast(inet '192.168.1.5/24')</code> → <code>192.168.1.255/24</code></p>                                                                                                                                                                                                                   |
| <p><code>family</code> ( <code>inet</code> ) → <code>integer</code></p><p>Returns the address's family: <code>4</code> for IPv4, <code>6</code> for IPv6.</p><p><code>family(inet '::1')</code> → <code>6</code></p>                                                                                                                                                                                                                          |
| <p><code>host</code> ( <code>inet</code> ) → <code>text</code></p><p>Returns the IP address as text, ignoring the netmask.</p><p><code>host(inet '192.168.1.0/24')</code> → <code>192.168.1.0</code></p>                                                                                                                                                                                                                                      |
| <p><code>hostmask</code> ( <code>inet</code> ) → <code>inet</code></p><p>Computes the host mask for the address's network.</p><p><code>hostmask(inet '192.168.23.20/30')</code> → <code>0.0.0.3</code></p>                                                                                                                                                                                                                                    |
| <p><code>inet\_merge</code> ( <code>inet</code>, <code>inet</code> ) → <code>cidr</code></p><p>Computes the smallest network that includes both of the given networks.</p><p><code>inet\_merge(inet '192.168.1.5/24', inet '192.168.2.5/24')</code> → <code>192.168.0.0/22</code></p>                                                                                                                                                         |
| <p><code>inet\_same\_family</code> ( <code>inet</code>, <code>inet</code> ) → <code>boolean</code></p><p>Tests whether the addresses belong to the same IP family.</p><p><code>inet\_same\_family(inet '192.168.1.5/24', inet '::1')</code> → <code>f</code></p>                                                                                                                                                                              |
| <p><code>masklen</code> ( <code>inet</code> ) → <code>integer</code></p><p>Returns the netmask length in bits.</p><p><code>masklen(inet '192.168.1.5/24')</code> → <code>24</code></p>                                                                                                                                                                                                                                                        |
| <p><code>netmask</code> ( <code>inet</code> ) → <code>inet</code></p><p>Computes the network mask for the address's network.</p><p><code>netmask(inet '192.168.1.5/24')</code> → <code>255.255.255.0</code></p>                                                                                                                                                                                                                               |
| <p><code>network</code> ( <code>inet</code> ) → <code>cidr</code></p><p>Returns the network part of the address, zeroing out whatever is to the right of the netmask. (This is equivalent to casting the value to <code>cidr</code>.)</p><p><code>network(inet '192.168.1.5/24')</code> → <code>192.168.1.0/24</code></p>                                                                                                                     |
| <p><code>set\_masklen</code> ( <code>inet</code>, <code>integer</code> ) → <code>inet</code></p><p>Sets the netmask length for an <code>inet</code> value. The address part does not change.</p><p><code>set\_masklen(inet '192.168.1.5/24', 16)</code> → <code>192.168.1.5/16</code></p>                                                                                                                                                     |
| <p><code>set\_masklen</code> ( <code>cidr</code>, <code>integer</code> ) → <code>cidr</code></p><p>Sets the netmask length for a <code>cidr</code> value. Address bits to the right of the new netmask are set to zero.</p><p><code>set\_masklen(cidr '192.168.1.0/24', 16)</code> → <code>192.168.0.0/16</code></p>                                                                                                                          |
| <p><code>text</code> ( <code>inet</code> ) → <code>text</code></p><p>Returns the unabbreviated IP address and netmask length as text. (This has the same result as an explicit cast to <code>text</code>.)</p><p><code>text(inet '192.168.1.5')</code> → <code>192.168.1.5/32</code></p>                                                                                                                                                      |

<br>

## Tip

The `abbrev`, `host`, and `text` functions are primarily intended to offer alternative display formats for IP addresses.

The MAC address types, `macaddr` and `macaddr8`, support the usual comparison operators shown in [Table 9.1](/the-sql-language/functions/functions-comparison.md#FUNCTIONS-COMPARISON-OP-TABLE) as well as the specialized functions shown in [Table 9.41](#MACADDR-FUNCTIONS-TABLE). In addition, they support the bitwise logical operators `~`, `&` and `|` (NOT, AND and OR), just as shown above for IP addresses.

**Table 9.41. MAC Address Functions**

| <p>Function</p><p>Description</p><p>Example(s)</p>                                                                                                                                                                                                                                                                                                 |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>trunc</code> ( <code>macaddr</code> ) → <code>macaddr</code></p><p>Sets the last 3 bytes of the address to zero. The remaining prefix can be associated with a particular manufacturer (using data not included in PostgreSQL).</p><p><code>trunc(macaddr '12:34:56:78:90:ab')</code> → <code>12:34:56:00:00:00</code></p>                |
| <p><code>trunc</code> ( <code>macaddr8</code> ) → <code>macaddr8</code></p><p>Sets the last 5 bytes of the address to zero. The remaining prefix can be associated with a particular manufacturer (using data not included in PostgreSQL).</p><p><code>trunc(macaddr8 '12:34:56:78:90:ab:cd:ef')</code> → <code>12:34:56:00:00:00:00:00</code></p> |
| <p><code>macaddr8\_set7bit</code> ( <code>macaddr8</code> ) → <code>macaddr8</code></p><p>Sets the 7th bit of the address to one, creating what is known as modified EUI-64, for inclusion in an IPv6 address.</p><p><code>macaddr8\_set7bit(macaddr8 '00:34:56:ab:cd:ef')</code> → <code>02:34:56:ff:fe:ab:cd:ef</code></p>                       |

<br>

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/functions-net.html)（英文原文，待翻譯）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.postgresql.tw/the-sql-language/functions/functions-net.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
