• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 501
  • Last Modified:

swift byte

Hi,


I have this easy question...but keep bugging so long...I don't know how exactly ">>" or "<<" will work...
I have a byte value...
Assume I have a value 0xAA and I would like to get two 0xA...how can I do so??  How can I switch my variables???

Help please~~~

Xenia
0
xenia27
Asked:
xenia27
  • 16
  • 15
  • 9
  • +1
2 Solutions
 
girionisCommented:
It is "shift" not "swift" :)

Shifting left means multiplying by 2, shifting right means dividing by two. To left-shift a number simply do:

13 << 1

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/bitwise.html
0
 
girionisCommented:
> Assume I have a value 0xAA and I would like to get two 0xA...how can I do so??  How can I switch my variables???

To multiply it by two just do:

int number =  0xAA;
System.out.println((number << 1));
0
 
thomas908Commented:
Left-shift << (JLS §15.19)
bits are shifted to the left based on the value of the right-operand
new right hand bits are zero filled
equivalent to left-operand times two to the power of the right-operand
For example, 16 << 5 = 16 * 25 = 512
Decimal 16            00000000000000000000000000010000

Left-shift 5     00000000000000000000000000010000    
  fill right     0000000000000000000000000001000000000
  discard left        00000000000000000000001000000000

the sign-bit is shifted to the left as well, so it can be dropped off or a different sign can replace it



Right-shift >> (JLS §15.19)
bits are shifted to the right based on value of right-operand
new left hand bits are filled with the value of the left-operand high-order bit therefore the sign of the left-hand operator is always retained
for non-negative integers, a right-shift is equivalent to dividing the left-hand operator by two to the power of the right-hand operator
For example: 16 >> 2 = 16 / 22 = 4
Decimal 16       00000000000000000000000000010000

Right-shift 2      00000000000000000000000000010000  
  fill left      00000000000000000000000000000100  
  discard right  00000000000000000000000000000100  -> Decimal 4
 
Decimal -16      11111111111111111111111111110000

Right-shift 2      11111111111111111111111111110000
  fill left      1111111111111111111111111111110000
  discard right  11111111111111111111111111111100  -> Decimal -4

Unsigned right-shift >>> (JLS §15.19)
identical to the right-shift operator only the left-bits are zero filled
because the left-operand high-order bit is not retained, the sign value can change
if the left-hand operand is positive, the result is the same as a right-shift
if the left-hand operand is negative, the result is equivalent to the left-hand operand right-shifted by the number indicated by the right-hand operand plus two left-shifted by the inverted value of the right-hand operand
For example: -16 >>> 2 = (-16 >> 2 ) + ( 2 << ~2 ) = 1,073,741,820
Decimal 16       00000000000000000000000000010000

Right-shift 2      00000000000000000000000000010000  
  fill left      00000000000000000000000000000100  
  discard right  00000000000000000000000000000100  -> Decimal 4

Decimal -16      11111111111111111111111111110000

>>> 2              11111111111111111111111111110000
  fill left      0011111111111111111111111111110000
  discard right  00111111111111111111111111111100  

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
thomas908Commented:
0
 
xenia27Author Commented:
sorry~  keep thinking "switch" insteading of "shift"...^^
0
 
thomas908Commented:
>>Assume I have a value 0xAA and I would like to get two 0xA...how can I do so

Sorry didn't get it.
Could u plz elaborate what u exactly mean by two oxA.

Do u mean u want to assign it to another variable?
0
 
girionisCommented:
For bytes you do exactly the same:

byte b = (byte) 0xAA;
System.out.println((b << 1));
0
 
girionisCommented:
thomas908 I think xenia means to multiply it by two.
0
 
thomas908Commented:
>>thomas908 I think xenia means to multiply it by two.

Then ur first post would do the trick
;)
0
 
xenia27Author Commented:
OK...this is what I mean...
Here is my situation...I enter a string "AAAAAAAA" and it will be stored into 4 bytes....then, I try to get this data, and I receive "-86" as I print out.  And I know "-86" equals to "FFFFFFAA"...so how can I transfer this value to the right value I need??

Hope this is clearer~  @@
0
 
thomas908Commented:
Which datatype are u using?
0
 
thomas908Commented:
Ur bits may be getting truncated getting because u may be using a smaller data type.
0
 
xenia27Author Commented:
the one I use to receive the information is a byte value...
0
 
thomas908Commented:
>>so how can I transfer this value to the right value I need??

I think rather than converting u'll have to save ur bits from getting truncated by using a large enough datatype
0
 
girionisCommented:
xenia, a byte is 8 bits long, meanign the the highest value you can store is 1111. The "AA" value you are trying to store is 10101010 which is more than 1111, so the value of the byte becomes negative.
0
 
thomas908Commented:
byte can store only 8 bits.
HAve u tried with int?
0
 
girionisCommented:
Bah, sorry the highest values is 11111111 (I need my coffee soon) :(
0
 
thomas908Commented:
oops! just a few seconds late ;)
0
 
xenia27Author Commented:
not sure why...i can the byte variable into "int" variable..and I still have "-86"...
0
 
girionisCommented:
Can you show us your code?
0
 
thomas908Commented:
can u post ut code/
0
 
girionisCommented:
If you do this:

System.out.println(((int) 0xAA));

you will get 170.
0
 
girionisCommented:
A is 1010 in binary, AA is 10101010 which is 128 + 32 + 8 + 2 = 170
0
 
xenia27Author Commented:
OK...I just realize why I still have "-86"...since the buffer I try to store my information into is a byte array...so when I parse the data, I still have "-86"...so is there any way I can get the correct value???? @@
0
 
thomas908Commented:
Can u make it an int array?
0
 
thomas908Commented:
U have to save the bits from getting truncated. Once they get truncated u can't get them back
0
 
xenia27Author Commented:
sorry...cannot do this...@@
it's a huge program...if I do so, I need to make tons of changes...
0
 
girionisCommented:
The byte array stores each character separately, not two together. The String "AAAAAAAA" will result in a byte array of length 8. Each location will have the character A which is represented as 0000000000001010. How do you store the data? Can you post some sample code?
0
 
thomas908Commented:
So the idea is use a large enough datatype.
0
 
thomas908Commented:
>>The byte array stores each character separately, not two together. The String "AAAAAAAA" will result in a byte array of length 8. Each location will have the character A which is represented as 0000000000001010. How do you store the data? Can you post some sample code

girionis is right. i missed the point
0
 
girionisCommented:
xenia please post the code where you do the byte stuff.
0
 
xenia27Author Commented:
OK...here is what I do...
First, I try to send my commands to a machine...so I send my string..."AAAAAAAA"
then, I need to convert the string to bytes...so I do this "tmpStr.getBytes()"...then, the machine returns a succeed signal...
so I try to update my program...then, what my program do is receive these data with a byte array...a buffer, then, I parse the information...so for this case, I need 4 bytes to store this string...so I have another byte array...byteArray[4]...and I would like to get the original string from this byte array...

Any idea how?
0
 
girionisCommented:
> .so for this case, I need 4 bytes to store this string.

No you do not. You need 8 bytes to store the string since you have 8 characters in a string.
0
 
girionisCommented:
If you do getBytes() on the string this will result in a byte array with length *eight not four*.
0
 
thomas908Commented:
ur string is 8 bytes long
0
 
girionisCommented:
Xenia, do not confuse the *byte* value 0xAA with the *string* value "AA". They are *not* the same.
0
 
xenia27Author Commented:
OK...I check my program....I'm wrong...I have an array with length 8... Sorry~
0
 
girionisCommented:
I think I know what went wrong in your mind. You have confused the unicode characters of java with the string characters. The "A" character is represented as unicode (2 bytes - 16 bits) but the "A" character is "one" character not two. So the string "AA" is "two" characters of 16 bits each (32 bits in total) and not one character.
0
 
CEHJCommented:
>>
OK...I just realize why I still have "-86"...since the buffer I try to store my information into is a byte array...so when I parse the data, I still have "-86"...so is there any way I can get the correct value???? @@
>>

If the buffer contains 0xAA, you can mask it, e.g.

byte[] buffer = { (byte)0xAA };
int value = buffer[0] & 0xFF;
System.out.println(value); // prints 170
0
 
xenia27Author Commented:
OK...what I try to do is try to print out "AA", which is a hex number, as a string...not int (170) or byte value...I totally understand what I have is hex value and "A" character in "AA" is part of the whole value...so that is why I wanna know how can I get two part of whole value separately by shifting byte...This is what I mean...sorry for misunderstanding...but since the original value in my buffer is incorrect...I don't know how can I do so...^^
0
 
girionisCommented:
Try this:

int number = 0xAA;
System.out.println(Integer.toHexString(number));
0
 
CEHJCommented:
>>but since the original value in my buffer is incorrect...I don't know how can I do so..

What do you mean by that exactly?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 16
  • 15
  • 9
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now