fixed read, write and write_bit function