Solved

swift byte

Posted on 2004-08-30
42
459 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
  • 16
  • 15
  • 9
  • +1
42 Comments
 
LVL 35

Assisted Solution

by:girionis
girionis earned 250 total points
Comment Utility
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
Comment Utility
> 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 250 total points
Comment Utility
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
 
LVL 8

Expert Comment

by:thomas908
Comment Utility
0
 

Author Comment

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

Expert Comment

by:thomas908
Comment Utility
>>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
Comment Utility
For bytes you do exactly the same:

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

Expert Comment

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

Expert Comment

by:thomas908
Comment Utility
>>thomas908 I think xenia means to multiply it by two.

Then ur first post would do the trick
;)
0
 

Author Comment

by:xenia27
Comment Utility
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
Comment Utility
Which datatype are u using?
0
 
LVL 8

Expert Comment

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

Author Comment

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

Expert Comment

by:thomas908
Comment Utility
>>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
Comment Utility
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
Comment Utility
byte can store only 8 bits.
HAve u tried with int?
0
 
LVL 35

Expert Comment

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

Expert Comment

by:thomas908
Comment Utility
oops! just a few seconds late ;)
0
 

Author Comment

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

Expert Comment

by:girionis
Comment Utility
Can you show us your code?
0
 
LVL 8

Expert Comment

by:thomas908
Comment Utility
can u post ut code/
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 35

Expert Comment

by:girionis
Comment Utility
If you do this:

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

you will get 170.
0
 
LVL 35

Expert Comment

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

Author Comment

by:xenia27
Comment Utility
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
Comment Utility
Can u make it an int array?
0
 
LVL 8

Expert Comment

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

Author Comment

by:xenia27
Comment Utility
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
Comment Utility
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
Comment Utility
So the idea is use a large enough datatype.
0
 
LVL 8

Expert Comment

by:thomas908
Comment Utility
>>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
Comment Utility
xenia please post the code where you do the byte stuff.
0
 

Author Comment

by:xenia27
Comment Utility
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
Comment Utility
> .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
Comment Utility
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
Comment Utility
ur string is 8 bytes long
0
 
LVL 35

Expert Comment

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

Author Comment

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

Expert Comment

by:girionis
Comment Utility
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
Comment Utility
>>
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
Comment Utility
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
Comment Utility
Try this:

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

Expert Comment

by:CEHJ
Comment Utility
>>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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoresSpecial  challenge 13 40
sumHeights2  challenge 7 75
firstChar challenge 13 83
mockito example issue 8 30
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now