From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id KAA03838 for caml-red; Fri, 1 Sep 2000 10:49:42 +0200 (MET DST) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id KAA02292 for ; Fri, 1 Sep 2000 10:21:25 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.10.0/8.10.0) with ESMTP id e818Knr06130; Fri, 1 Sep 2000 10:20:49 +0200 (MET DST) Received: (from xleroy@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id KAA01481; Fri, 1 Sep 2000 10:20:48 +0200 (MET DST) Message-ID: <20000901102048.28072@pauillac.inria.fr> Date: Fri, 1 Sep 2000 10:20:48 +0200 From: Xavier Leroy To: Bomshik Kim , caml-list@inria.fr Subject: Re: How can I treat bits? References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 0.89.1 In-Reply-To: ; from Bomshik Kim on Thu, Aug 31, 2000 at 10:13:14PM +0900 Sender: weis@pauillac.inria.fr > 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