[5342] in java-interest
Re: Questions with signed variables and boolean operations
daemon@ATHENA.MIT.EDU (Walter Szewelanczyk)
Tue Feb 6 10:40:00 1996
From: "Walter Szewelanczyk" <Walter@adco.com>
To: java-interest@java.sun.com
Date: Mon, 5 Feb 1996 11:29:06 +0000
Hi again,.
I just wrote :
> It appears that when doing binary bitwise operations you want to make
> sure that the data types are of the same type or you might get
> unexpected results. I was trying to "or" a byte to the bottom 8 bits
> of an int. Everything was fine until the byte had its most
> siginificant bit set. I.e it was a negative number. What I am
> assuming happened is that the signed byte is being converted to a
> signed int and then the "or" operation takes place. As you might
> expect this is not what I wanted. Since I could not find any
> referance to (and the compiler did not like) unsigned data types I
> have a simple solution;
>
> Firts you assign the byte that you need to perform the binary
> operation with to an int. You then mask off the top 24 bits with a
> bitwise "and" operation. You can now do what you nedd with the temp;
>
> int big = 0;
> byte little = -112;
> big |= little //----------> big now == -112
>
> big = 0;
> int temp = little;
> temp &= 0x000000FF;
> big |= temp //--------------> big now equals 144 as desired
>
> hopefully this was helpfull for someone,
>
> Walt
I hjust wanted to point out that,
The anding with 0x000000FF would only work with "or" type operations
if you need to "and" a byte the you would need to "or" the temp int
with 0xFFFFFF00, the inverted mask from before.
ie
int temp = little;
temp |= 0xFFFFFF00;
big &= temp
didn't want to mislead anyone,
Sorry
Walt
********************************************************************************************
Walter Szewelanczyk Technical Director
Walter@adco.com NET VENTURE, Inc.
http://www.adco.com
"One must stay in a very RIGID state of FLEXABILITY"
********************************************************************************************
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com