Two Laptop Bag
The moose likes Java in General and the fly likes short to bit ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "short to bit ?" Watch "short to bit ?" New topic
Author

short to bit ?

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Hello,

Is there a method to convert a short-value to bit-value?

I want to do something like:



So you see, I want to add the bit value of a and b and convert the summary to a short value.

How can I do that?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16680
    
  19

What do you mean by "bit value"? Do you mean you want a specific bit?

If you do, the easiest way is to shift the bit, and AND out the rest of the bits.

For example, if you want the fourth bit of the short...



or if you don't want to shift ...



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
for example:

Short a = 2; // in byte: 0000'0010
Short b = 8; // in byte 0000'1000

the sum is: 0000'0010+0000'1000 = 0000'1010, which is in (decimal): 10

So you see, I want to sum the byte value of two short values and convert the byte value to decimal value (in my case, a short).

How can I do that in Java?


By the way:
Byte byte = a.byteValue()*8; // byte is "2" instead of "0000'0010", why is that so?

// for what is 0x10 ??
boolean bit = ((2 & 0x10) == 0x10); // this returns false, so bit=0




Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16680
    
  19

nimo frey wrote:for example:

Short a = 2; // in byte: 0000'0010
Short b = 8; // in byte 0000'1000

the sum is: 0000'0010+0000'1000 = 0000'1010, which is in (decimal): 10

So you see, I want to sum the byte value of two short values and convert the byte value to decimal value (in my case, a short).

How can I do that in Java?


I am definitely missing something here.... 2 + 8 = 10

That is how numbers are added in Java. How is what you are describing any different? What's wrong with...

sum = a + b;

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16680
    
  19

By the way:
Byte byte = a.byteValue()*8; // byte is "2" instead of "0000'0010", why is that so?


I think you are confusing a byte with how it is displayed. A byte is a 8 bit value -- that is all. Whether that value is displayed (when you print it) as "2", or "0x2", or "0000'0010" depends on how you display it. In all three cases, the byte has the same value -- there is no "instead".

Henry
Dave Brown
Ranch Hand

Joined: Mar 08, 2005
Posts: 301
Maybe the loss of precission compiler error is what's deterring the OP.. An addition will result in an int... so if you really needed a short as the result you must cast the addition i.e. short sum = (short) (a + b);

Dave.


Henry Wong wrote:
nimo frey wrote:for example:

Short a = 2; // in byte: 0000'0010
Short b = 8; // in byte 0000'1000

the sum is: 0000'0010+0000'1000 = 0000'1010, which is in (decimal): 10

So you see, I want to sum the byte value of two short values and convert the byte value to decimal value (in my case, a short).

How can I do that in Java?


I am definitely missing something here.... 2 + 8 = 10

That is how numbers are added in Java. How is what you are describing any different? What's wrong with...

sum = a + b;

Henry


Regards, Dave Brown
SCJP 6 - [url]http://www.dbws.net/[/url] - Check out Grails Forum
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16680
    
  19

// for what is 0x10 ??
boolean bit = ((2 & 0x10) == 0x10); // this returns false, so bit=0


0x10 is the hexidecimal representation for 16, which is the fourth bit, ie... 0001'0000 in binary representation. And yeah, with 2, the fourth bit is zero.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16680
    
  19

Dave Brown wrote:Maybe the loss of precission compiler error is what's deterring the OP.. An addition will result in an int... so if you really needed a short as the result you must cast the addition i.e. short sum = (short) a + b;

Dave.


Yeah, we will definitely need some clarification from the OP.

Henry
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
Dave Brown wrote: . . . short sum = (short) a + b;
If you want a short, surely you mean short sum = (short) (a + b); ?
Dave Brown
Ranch Hand

Joined: Mar 08, 2005
Posts: 301
Campbell Ritchie wrote:
Dave Brown wrote: . . . short sum = (short) a + b;
If you want a short, surely you mean short sum = (short) (a + b); ?


yes thats what I mean, and stop calling me Shirly ;-)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
Dave Brown wrote: . . . stop calling me Shirly ;-)
I lost a pair of glasses today, so I can't understand that joke any more
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I have a couple of questions:

Imagine, I want the SECOND Bit beginning from the RIGHT Side of a Short-Value:

Short: 2
In Bits(Dualsystem): 0000'0010 (1 is the second bit beginning from the right side)

Is this shift-operation right, to get the second bit beginning searching from the right side ?





Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
Because shift operations do not produce a single bit. Look at this old thread.

If you want a particular bit try
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
thank you, the thread is good and helped me to solve my problem!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
You're welcome
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: short to bit ?
 
Similar Threads
casting
(#61) Can you automatically/implicitly convert a char to a short?
reading a multibyte value from a file
How can I convert the xml to a string?
Float.POSITIVE_INFINITY...?