* How can I treat bits?
@ 2000-08-31 13:13 Bomshik Kim
2000-09-01 8:20 ` Xavier Leroy
0 siblings, 1 reply; 2+ messages in thread
From: Bomshik Kim @ 2000-08-31 13:13 UTC (permalink / raw)
To: caml-list
In C language, we can define a variable size as a unit of bits.
For example, "unsigned int a:1 ;", " unsigned int b:4 ;" ....
"colon" is used to set up the number of bits.
Can I define OCaml-variables in the same way?
Because I want to make some data header by using bits as little as
possible.
the style that I imagine is...
type hd = { flag : int_1 ; on_off : int_1 ; seq_num : int_4 } ;;
let header = { flag = 1 ; on_off = 0 ; seq_num = 0101 } ;;
Thank you.
-BS Kim
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: How can I treat bits?
2000-08-31 13:13 How can I treat bits? Bomshik Kim
@ 2000-09-01 8:20 ` Xavier Leroy
0 siblings, 0 replies; 2+ messages in thread
From: Xavier Leroy @ 2000-09-01 8:20 UTC (permalink / raw)
To: Bomshik Kim, caml-list
> In C language, we can define a variable size as a unit of bits.
> For example, "unsigned int a:1 ;", " unsigned int b:4 ;" ....
> "colon" is used to set up the number of bits.
>
> Can I define OCaml-variables in the same way?
No, you can't. The smallest unit for data representations is the
machine word. You can also work at the level of bytes by using
strings as byte arrays.
> Because I want to make some data header by using bits as little as
> possible.
> the style that I imagine is...
> type hd = { flag : int_1 ; on_off : int_1 ; seq_num : int_4 } ;;
> let header = { flag = 1 ; on_off = 0 ; seq_num = 0101 } ;;
The best you could do is represent hd as an integer and define
constructor and accessors yourself:
type hd = int
let make_hd flag on_off seq_num =
flag lor (on_off lsl 1) lor (seq_num lsl 2)
let flag_hd h = h land 1
let on_off_hd h = (h lsr 1) land 1
let seq_num_hd h = (h lsr 2) land 0xF
- Xavier Leroy
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2000-09-01 8:49 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-08-31 13:13 How can I treat bits? Bomshik Kim
2000-09-01 8:20 ` Xavier Leroy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox