+
+/* Return the value of BITSTRING[IDX] as (boolean) type TYPE. */
+
+struct value *
+value_bitstring_subscript (struct type *type,
+ struct value *bitstring, struct value *idx)
+{
+
+ struct type *bitstring_type, *range_type;
+ LONGEST index = value_as_long (idx);
+ struct value *v;
+ int offset, byte, bit_index;
+ LONGEST lowerbound, upperbound;
+
+ bitstring_type = check_typedef (value_type (bitstring));
+ gdb_assert (TYPE_CODE (bitstring_type) == TYPE_CODE_BITSTRING);
+
+ range_type = TYPE_INDEX_TYPE (bitstring_type);
+ get_discrete_bounds (range_type, &lowerbound, &upperbound);
+ if (index < lowerbound || index > upperbound)
+ error (_("bitstring index out of range"));
+
+ index -= lowerbound;
+ offset = index / TARGET_CHAR_BIT;
+ byte = *((char *) value_contents (bitstring) + offset);
+
+ bit_index = index % TARGET_CHAR_BIT;
+ byte >>= (gdbarch_bits_big_endian (current_gdbarch) ?
+ TARGET_CHAR_BIT - 1 - bit_index : bit_index);
+
+ v = value_from_longest (type, byte & 1);
+
+ set_value_bitpos (v, bit_index);
+ set_value_bitsize (v, 1);
+
+ VALUE_LVAL (v) = VALUE_LVAL (bitstring);
+ if (VALUE_LVAL (bitstring) == lval_internalvar)
+ VALUE_LVAL (v) = lval_internalvar_component;
+ VALUE_ADDRESS (v) = VALUE_ADDRESS (bitstring);
+ VALUE_FRAME_ID (v) = VALUE_FRAME_ID (bitstring);
+
+ set_value_offset (v, offset + value_offset (bitstring));
+
+ return v;
+}
+