Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Advanced-java] Simple byte question
- From: Brian.Cain@xxxxxxxxxxxx (Cain Brian-BCAIN1)
- Subject: [Advanced-java] Simple byte question
- Date: Mon, 28 Jan 2002 13:43:59 -0600
> -----Original Message----- > From: Jason Boyd [mailto:jboyd@xxxxxxxxxxxxxxxx] > > If (byte) 0x10 == 0001 0000 > (byte) 0X20 == 0010 0000 > (byte) 0x40 == 0100 0000 > > What is the equivalent of 1000 0000? Is "1000 0000" in two's complement? > Is it -0x01 ? -0xFF ? I believe it's 0x80. I've never tried the arithmetic negation operator with hex, but I don't know if it's going to do what you want it to. You should try it to see what it does though. Java does try to give you some heads up, because when you try to assign a literal that sets the sign bit to a byte, it forces you to cast it first (so that you're aware of things like sign extension, I guess). Since all data types are signed, you can do bitwise operations on a byte that contains "0x80", and it will contain the leftmost bit set to one, all others set to zero. > Part 2: How does one use unsigned types within Java without > such trickery? Dunno. When using network protocols, I try to abstract things when I can with DataIn/OutputStreams (which really do bitmasks and shifts, but conveniently hide them from view). Otherwise, you just have to deal with the fact that it's signed, I believe. If I want 32 unsigned bits, I use a long (but only if it represents a quantity). If I need 32 bits for a unique identifier, then it doesn't matter whether they're signed or not. I think I considered using BigDecimal for a while, since it seemed to acknowledge some things about sign, but that's usually like using a hammer to crack a nut. Arbitrary precision is not necessary. If someone else knows a good workaround, I'm curious too. -Brian
- Prev by Date: [Advanced-java] Simple byte question
- Next by Date: [Advanced-java] NYJavaSIG - Feb 4, 2002 Meeting - Inside Limewire - Java Meets Gnutella
- Previous by thread: [Advanced-java] Simple byte question
- Next by thread: [Advanced-java] NYJavaSIG - Feb 4, 2002 Meeting - Inside Limewire - Java Meets Gnutella
- Index(es):