Memory layout of a data structure: Difference between revisions
Content added Content deleted
(Go solution) |
|||
Line 257: | Line 257: | ||
Of course, this is a very simplified view of the full RS-232 protocol. Also, although this represents the order of the pins in a D-9 connector, this would not necessarily be the same as the order of the bits in a control register. |
Of course, this is a very simplified view of the full RS-232 protocol. Also, although this represents the order of the pins in a D-9 connector, this would not necessarily be the same as the order of the bits in a control register. |
||
=={{header|Go}}== |
|||
Go does not have named bits as part of the type system. Instead, constants are typically defined as shown. For a word of bits with special meanings like this, a type would be defined though, as shown. Static typing rules then control assignments and comparisons at the word level. At the bit level, it helps to follow naming conventions so that, say, using a 9-pin constant on a 25-pin word would be an obvious error in the source code. |
|||
<lang go>package main |
|||
import "fmt" |
|||
type rs232p9 uint16 |
|||
const ( |
|||
CD9 = 1 << iota // Carrier detect |
|||
RD9 // Received data |
|||
TD9 // Transmitted data |
|||
DTR9 // Data terminal ready |
|||
SG9 // signal ground |
|||
DSR9 // Data set ready |
|||
RTS9 // Request to send |
|||
CTS9 // Clear to send |
|||
RI9 // Ring indicator |
|||
) |
|||
func main() { |
|||
// set some nonsense bits just for example |
|||
var p rs232p9 = RI9 | TD9 | CD9 |
|||
fmt.Printf("%04x\n", p) |
|||
}</lang> |
|||
Output: |
|||
<pre> |
|||
0105 |
|||
</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |