# Std::bitset streamer: endianness issue?

**URL:** <https://root-forum.cern.ch/t/std-bitset-streamer-endianness-issue/41529>\
**Category:** ROOT\
**Tags:** io\
**Created:** [September 21, 2020, 12:54pm UTC](https://root-forum.cern.ch/t/std-bitset-streamer-endianness-issue/41529 "2020-09-21T12:54:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sbinet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sbinet/32/13166_2.png) [@sbinet](https://root-forum.cern.ch/u/sbinet)\
**Post date:** [September 21, 2020, 12:54pm UTC](https://root-forum.cern.ch/t/std-bitset-streamer-endianness-issue/41529/1 "2020-09-21T12:54:57Z")

</div>

hi there,

I was adding support for reading/writing `std::bitset<N>` to [groot](https://go-hep.org/x/hep/groot) when I noticed the following behaviour:

```auto
$> cat ./Event.h
#ifndef MYEVT_H
#define MYEVT_H 1

#include <bitset>

struct Event {
	std::bitset<16> Bs;
};

#endif // MYEVT_H

$> cat ./run.C
#include "Event.h"

void run() {
	gSystem->Load("./libEvent.so");

	auto f = TFile::Open("std-bitset.root", "RECREATE");
	auto t = new TTree("tree", "tree");

	int bufsize = 32000;
	int splitlvl = 99;

	Event e;
	e.Bs = std::bitset<16>("0001010111101010");

	t->Branch("evt", &e, bufsize, splitlvl);

	t->Fill();
	f->Write();
	f->Close();

	exit(0);
}

$> root -b -q ./run.C

```

reading back the file with `groot`, I see the following bit patterns:

```auto
$> root-dump testdata/std-bitset.root 
>>> file[testdata/std-bitset.root]
key[000]: tree;1 "tree" (TTree)
[000][evt]: {[0 1 0 1 0 1 1 1 1 0 1 0 1 0 0 0]}

```

_ie:_ it seems endianness of the bitset isn’t handled when writing the bitset (and it’s whatever it happens to be on the writing machine).  
(I was expecting: `[0 0 0 1 0 1 0 1 1 1 1 0 1 0 1 0]` as the on-disk bytes)

or are `bitset<N>` values always little-endian encoded?

thx,  
-s

* * *

_Please read [tips for efficient and successful posting](https://root-forum.cern.ch/t/tips-for-efficient-and-successful-posting/28292) and [posting code](https://root-forum.cern.ch/t/posting-code-read-this-first/28293)_

ROOT Version: 6.22/02  
Built for linuxx8664gcc on Aug 17 2020, 12:46:52  
From tags/v6-22-02@v6-22-02

* * *

---

<div class="post-metadata">

**Author:** ![sbinet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sbinet/32/13166_2.png) [@sbinet](https://root-forum.cern.ch/u/sbinet)\
**Post date:** [September 21, 2020, 7:27pm UTC](https://root-forum.cern.ch/t/std-bitset-streamer-endianness-issue/41529/2 "2020-09-21T19:27:11Z")

</div>

if somebody (@pcanal?) could test on a big-endian machine?

---

<div class="post-metadata">

**Author:** ![sbinet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sbinet/32/13166_2.png) [@sbinet](https://root-forum.cern.ch/u/sbinet)\
**Post date:** [September 22, 2020, 7:58am UTC](https://root-forum.cern.ch/t/std-bitset-streamer-endianness-issue/41529/3 "2020-09-22T07:58:06Z")

</div>

I was also a bit surprised to see a `std::bitset<N>` to take (N+4)bytes on disk (instead of “just” N **bits** +4bytes, or, even, just N **bits** ).

is there an underlying reason I am missing here? (it’s also quite possible I am completely mistaken, of course)

---

<div class="post-metadata">

**Author:** ![sbinet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sbinet/32/13166_2.png) [@sbinet](https://root-forum.cern.ch/u/sbinet)\
**Post date:** [September 24, 2020, 10:45am UTC](https://root-forum.cern.ch/t/std-bitset-streamer-endianness-issue/41529/4 "2020-09-24T10:45:42Z")

</div>

I’ve created an issue ([root-project/root#6412](https://github.com/root-project/root/issues/6412)) about this (both the endian-ness and the probably inefficient storage), but it’d be great to have some feedback about this. (either here or over there)

thanks.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [September 25, 2020, 9:12pm UTC](https://root-forum.cern.ch/t/std-bitset-streamer-endianness-issue/41529/5 "2020-09-25T21:12:27Z")

</div>

> [@sbinet](#):
>
> big-endian machine

humm … I should be able to do that … i’ll check next week.

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [October 9, 2020, 9:12pm UTC](https://root-forum.cern.ch/t/std-bitset-streamer-endianness-issue/41529/6 "2020-10-09T21:12:29Z")

</div>

Closed on October 9, 2020, 9:12pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
