• Post Reply Bookmark Topic Watch Topic
  • New Topic

short to bit ?  RSS feed

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// 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
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you, the thread is good and helped me to solve my problem!!
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!