Hi all, I had a post relating to this topic a few weeks back where i wanted to turn two bytes into a character. Now i want to turn a character into two bytes. I have seen this done by someone else and i have a quick question on it. I first cast my character to an int. char charA = "A" int a = ( int ) charA; To put bits 8 to 15 into a byte i can do the following byte firstByte = ( byte ) (a >> 8); Then to put bits 7 to 0 into a byte i can do the following byte secondByte = ( byte ) a; As far as i can make out this gives me a correct solution. Howerver i have seen a solution where the second byte is calculated as follows: byte secondByte = ( byte )( a & ooff ); I dont understand the purpose of AND'ing the value of a by 256. Will casting a to a byte not give me the first 8 bits. The maximum value that can be stored in 8 bits is 256 so why AND it. For example if the character 'A' were held in an int, that int would have the binary representation 01000001. AND'ing this by 256 11111111 would leave me with 01000001, the original value. So can anyone give me a reason for AND'ing with 256. Cheers, John [ February 06, 2003: Message edited by: John Ryan ]
Chris Allen
Ranch Hand
Joined: Feb 01, 2003
Posts: 127
posted
0
Just a shot in the dark here but I am wondering if the AND is performed in order to maintain the significant digit. In this example it won't matter because the character is always a positive integer but what about a case where you are trying to split a negative integer into two bytes? Either way, I think your solution is acceptable.
John Ryan
Ranch Hand
Joined: Mar 14, 2001
Posts: 124
posted
0
Originally posted by Chris Allen: Just a shot in the dark here but I am wondering if the AND is performed in order to maintain the significant digit. In this example it won't matter because the character is always a positive integer but what about a case where you are trying to split a negative integer into two bytes? Either way, I think your solution is acceptable.
Well when converting a character the int will never be negative so there would be no need to do this check
I have seen (and use this myself) when converting numeric values (rather than character values) to byte for the reason as stated in the first reply. when converting a character to a byte I wouldn't see this a necessary. I don't know if this would be a problem if you use other special characters rather than just simple alpha-numerics however...
Steve
Maulin Vasavada
Ranch Hand
Joined: Nov 04, 2001
Posts: 1863
posted
0
hi Steve,
I have seen (and use this myself) when converting numeric values (rather than character values) to byte for the reason as stated in the first reply
do u mean - "maintaining significant bit"? where do we maintain it? i don't see it. and in the same reply Chris mentions in the last that,
Either way, I think your solution is acceptable.
which means we don't need "&" useage. if we r casiting something to byte thats it. we only consider least significant byte. why we care what the sign was? (anyways we are killing the person then why care about if he had two or one hand? ) let me know if i'm missing anything important here... regards maulin
The cause of the confusion is, that 0xff doesn't equal 256, but 255 instead. 255 represented in binary is 11111111. If you'd bitwise-and any integer with this value, the result would be the least significant byte of the original number. Here's a small example:
but of course, casting an int to a byte type, as in 'byte lo= (byte)num' chops off all but the least significant byte also. kind regards