Check out Manning's Countdown to 2014. Use discount code crdotd14 all month for 50% off every deal.
Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# 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: 17626

33

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

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: 17626

33

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: 17626

33

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: 17626

33

// 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: 17626

33

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: 34567

13
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: 34567

13
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: 34567

13
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: 34567

13
You're welcome

Don't get me started about those stupid light bulbs.

subject: short to bit ?