The problem you're seeing is again due to bytes being a signed number, in particular because widening conversions preserve the sign, thus left-padding with ones if the MSB is one (i.e., a negative number). Since bit-shift-left converts the byte to an int, the 0xFF byte becomes a 0xFFFFFFFF int. When you start with ints instead of bytes, the int is just 0xFF, thus no leading ones.
If you're playing with bytes, you always have to bit-mask the widening conversions to deal with sign issues. In fact you handled this in your original java code by using &0xFF against each byte prior to shifting. On Mar 25, 3:54 pm, Raph <mart...@gmail.com> wrote: > On Mar 24, 10:57 am, "Mark J. Reed" <markjr...@gmail.com> wrote: > > > On Tue, Mar 23, 2010 at 8:19 PM, Raph <mart...@gmail.com> wrote: > > > (My opinion, anyway.I think a byte should be 8 bits and I should be able > > > to use all of them.) > > > Er, it is, and you can. A Java byte still gives you all 8 bits' worth > > of 256 different possible values; the interpretation of those values > > is all that differs here. Whereas C lets you pick between signed and > > unsigned (with the default unfortunately not always well-defined), > > Java gives you no choice but to use the signed interpretation. But > > you still get to use all 8 bits of the byte; it's just that the > > numbers mapped to [128, 255] in unsigned interpretations map to > > [-128,-1] instead. > > Right, should have been more specific. The 0xFF byte doesn't work the > way I expect it to. I have to use ints to get the correct answer. > > (bit-or (bit-shift-left (byte 0x01) 16) > (bit-shift-left (byte 0x7F) 8)) => 98048 > > (bit-or (bit-shift-left (int 0x01) 16) > (bit-shift-left (int 0x7F) 8)) => 98048 > > But... > > (bit-or (bit-shift-left (byte 0x01) 16) > (bit-shift-left (byte 0xFF) 8)) => -256 > > (bit-or (bit-shift-left (int 0x01) 16) > (bit-shift-left (int 0xFF) 8)) => 130816 > > So I can't use the bits the way I'd expect. > > Raph -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en To unsubscribe from this group, send email to clojure+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.