I'm working on a 'tabindex' defect in an application that works fine in Firefox and Chrome, but not Internet Explorer. The 'tabindex' is set through a JSP by a static 'int', which is incremented each time it is used.
private static short tabIndex = 1;
sb.append("<select tabindex='").append(tabIndex++) ...
Internet Explorer has a maximum 'tabindex' of 32767 (Short.MAX_VALUE
). So I can change the int
to a short
, but after it reaches the maximum, it will tick around to Short.MIN_VALUE
.
Objects with a negative tabIndex are omitted from the tabbing order.
So I need it to progress from Short.MAX_VALUE
to zero.
Obviously, it is trivial to write some code to do this, but I wondered if there is some clever short-hand way that I can increment to a strictly positive number, or some other data type I can use.
You can make sure that the MSB is always 0 by ANDing the value with 0x7FFF
.
short s = 32767;
short s1 = (short)(s & 0x7FFF); // s1 = 32767 (or 0x7FFF)
s++; // s = -32768 (or 0x8000)
short s2 = (short)(s & 0x7FFF); // s2 = 0
You might want to extract the incrementation and bitmasking to its own method and just use that inside append
.
I'm not condoning this code, because it's not very clear and frankly returning from assignment is not something I'd use, but the following would be a very short version:
public short incIndex() {
return tabIndex = (short)(tabIndex+1 & 0x7FFF);
}
...append(incIndex())...