Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

swift byte

Posted on 2004-08-30
42
Medium Priority
?
499 Views
Last Modified: 2010-03-31
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
Comment
Question by:xenia27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 16
  • 15
  • 9
  • +1
42 Comments
 
LVL 35

Assisted Solution

by:girionis
girionis earned 1000 total points
ID: 11929164
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
 
LVL 35

Expert Comment

by:girionis
ID: 11929173
> 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
 
LVL 8

Accepted Solution

by:
thomas908 earned 1000 total points
ID: 11929180
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!

 
LVL 8

Expert Comment

by:thomas908
ID: 11929182
0
 

Author Comment

by:xenia27
ID: 11929183
sorry~  keep thinking "switch" insteading of "shift"...^^
0
 
LVL 8

Expert Comment

by:thomas908
ID: 11929187
>>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
 
LVL 35

Expert Comment

by:girionis
ID: 11929190
For bytes you do exactly the same:

byte b = (byte) 0xAA;
System.out.println((b << 1));
0
 
LVL 35

Expert Comment

by:girionis
ID: 11929193
thomas908 I think xenia means to multiply it by two.
0
 
LVL 8

Expert Comment

by:thomas908
ID: 11929203
>>thomas908 I think xenia means to multiply it by two.

Then ur first post would do the trick
;)
0
 

Author Comment

by:xenia27
ID: 11929210
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
 
LVL 8

Expert Comment

by:thomas908
ID: 11929228
Which datatype are u using?
0
 
LVL 8

Expert Comment

by:thomas908
ID: 11929230
Ur bits may be getting truncated getting because u may be using a smaller data type.
0
 

Author Comment

by:xenia27
ID: 11929232
the one I use to receive the information is a byte value...
0
 
LVL 8

Expert Comment

by:thomas908
ID: 11929235
>>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
 
LVL 35

Expert Comment

by:girionis
ID: 11929237
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
 
LVL 8

Expert Comment

by:thomas908
ID: 11929238
byte can store only 8 bits.
HAve u tried with int?
0
 
LVL 35

Expert Comment

by:girionis
ID: 11929240
Bah, sorry the highest values is 11111111 (I need my coffee soon) :(
0
 
LVL 8

Expert Comment

by:thomas908
ID: 11929242
oops! just a few seconds late ;)
0
 

Author Comment

by:xenia27
ID: 11929264
not sure why...i can the byte variable into "int" variable..and I still have "-86"...
0
 
LVL 35

Expert Comment

by:girionis
ID: 11929267
Can you show us your code?
0
 
LVL 8

Expert Comment

by:thomas908
ID: 11929270
can u post ut code/
0
 
LVL 35

Expert Comment

by:girionis
ID: 11929274
If you do this:

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

you will get 170.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11929284
A is 1010 in binary, AA is 10101010 which is 128 + 32 + 8 + 2 = 170
0
 

Author Comment

by:xenia27
ID: 11929285
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
 
LVL 8

Expert Comment

by:thomas908
ID: 11929293
Can u make it an int array?
0
 
LVL 8

Expert Comment

by:thomas908
ID: 11929296
U have to save the bits from getting truncated. Once they get truncated u can't get them back
0
 

Author Comment

by:xenia27
ID: 11929297
sorry...cannot do this...@@
it's a huge program...if I do so, I need to make tons of changes...
0
 
LVL 35

Expert Comment

by:girionis
ID: 11929298
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
 
LVL 8

Expert Comment

by:thomas908
ID: 11929299
So the idea is use a large enough datatype.
0
 
LVL 8

Expert Comment

by:thomas908
ID: 11929304
>>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
 
LVL 35

Expert Comment

by:girionis
ID: 11929315
xenia please post the code where you do the byte stuff.
0
 

Author Comment

by:xenia27
ID: 11929318
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
 
LVL 35

Expert Comment

by:girionis
ID: 11929332
> .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
 
LVL 35

Expert Comment

by:girionis
ID: 11929334
If you do getBytes() on the string this will result in a byte array with length *eight not four*.
0
 
LVL 8

Expert Comment

by:thomas908
ID: 11929336
ur string is 8 bytes long
0
 
LVL 35

Expert Comment

by:girionis
ID: 11929340
Xenia, do not confuse the *byte* value 0xAA with the *string* value "AA". They are *not* the same.
0
 

Author Comment

by:xenia27
ID: 11929343
OK...I check my program....I'm wrong...I have an array with length 8... Sorry~
0
 
LVL 35

Expert Comment

by:girionis
ID: 11929359
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11931958
>>
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
 

Author Comment

by:xenia27
ID: 11938253
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
 
LVL 35

Expert Comment

by:girionis
ID: 11939257
Try this:

int number = 0xAA;
System.out.println(Integer.toHexString(number));
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11939638
>>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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question