2

I am new to bytes and bits and I'm also not very good in maths either. I kind of understand the bitwise operators, but I don't know how to solve math equations/formulas with 2 variables. I'm not sure this is the proper place to ask this but anyway.

I have an formula like this:

(Adr_MSB & 0x3F) << (8 + Adr_LSB)

Now what I want is that I would get an integer (for example 33) and the code would transform it into Adr_MSB and Adr_LSB (which are bytes). It should work up to 128 (ok I guess it will be 127).

I know that this question might sound dumb or something, but I just don't know enough maths to solve this.

Thanks for all help.

EDIT: By experimenting I figured out, that Adr_MSB is a multiplier (e.g. if it's 10 the result is 10 times biger than if it's 1).

grizeldi
  • 180
  • 2
  • 12
  • Related: http://stackoverflow.com/questions/7199625/mathematical-equation-for-and-bitwise-operation –  Aug 17 '15 at 15:58
  • Is this a programming question? Are you perhaps looking for http://math.stackexchange.com/ instead? – tnw Aug 17 '15 at 16:01
  • Yes that's I've been thinking by saying that IDK if this is a proper place. But since it contains bitwise operators I think that this is a proper place. – grizeldi Aug 17 '15 at 16:05
  • 2
    @tnw bit arithmetic is usually not considered a math problem. – RealSkeptic Aug 17 '15 at 16:05
  • So to clarify: You are looking for an "inverse" of this expression? That is, if you have an input like 33, then you want to compute Adr_MSB and Adr_LSB so that the posted expression gives 33 as the result? – Marco13 Aug 17 '15 at 16:09
  • @Marco13 yes that's it – grizeldi Aug 17 '15 at 16:10
  • First, what you have there is a formula, not an equation. An equation has a `=` sign in it, and is used to describe a "fact" about its components, whereas this formula is a way to derive a value from the given components. So what you are asking is a formula (expression) that will give you the reverse. In fact, it will be two expressions, as you need two values. But anyway, if you wish to be a programmer, the best way for you is to study bit arithmetic, not to ask for ready solutions on the Internet. – RealSkeptic Aug 17 '15 at 16:13
  • Thanks for info, I changed the title, but studying a bit of arithmetic is not a holiday hobby for a 15 year old. – grizeldi Aug 17 '15 at 16:15
  • 1
    (Once more, I wish I could downvote comments. It **should** be a hobby. Otherwise, programming may not be the right field for you. Just my 2 cents) – Marco13 Aug 17 '15 at 16:16
  • I was talking about the arithmetic of bits, not about a bit of arithmetic. And you might be surprised, but it is actually the holiday hobby of many 15 year olds. – RealSkeptic Aug 17 '15 at 16:16
  • @RealSkeptic True, I just don't see how this is a java question. – tnw Aug 17 '15 at 16:16
  • @tnw not really a java question but need a java answer. – grizeldi Aug 17 '15 at 16:17
  • @tnw The expression he used is a valid Java expression and I assume he wants the answer in valid Java expressions. In any case, it's probably not in the domain of math.stackexchange.com. – RealSkeptic Aug 17 '15 at 16:18
  • @RealSkeptic I really should go and learn some bit arithmetic, but all I can find is just some basic info and no good tutorials... About maths as a hobby... Well I guess that exists too, but I personally don't know anyone like that. – grizeldi Aug 17 '15 at 16:19
  • That's because they usually only sit at home, studying bit arithmetic ;-) However, I think that the question and scope are clear now, so probably no need for further off-topic discussion. Back to topic: The results are not unique. For an input of 1024, you can have MSB,LSB values (1,2), or (2,1), or (4,0). Even when considering that for the MSB, only values 0,1,2 and 3 make sense, there will be no unique solution.... – Marco13 Aug 17 '15 at 16:22
  • Hmm... I'm pretty sure this is possible to solve since the thing I'm implementing has already been implemented, but not relased in a way I can use. – grizeldi Aug 17 '15 at 16:25
  • No, actually, @Marco13 is right. Even the `Adr_MSB & 0x3F` part has four possible values of `Adr_MSB` that would produce the same value. And if that value is even, it's impossible to know the amount of shift that was done in the `<<` part. – RealSkeptic Aug 17 '15 at 16:40
  • If you think that it is possible: What should the result be when the input is 512? Hint: It could be (1,1) or (2,0). You simply don't know. Without further constraints (e.g. on the maximum number that will be used as the input), this can not be solved properly. But if 63 is sufficient (as suggested in one answer), then this may be OK... – Marco13 Aug 17 '15 at 16:51
  • Another side note: The expression can not result in a value like 33. In fact, it can take only very few values. But all this does not seem to matter. – Marco13 Aug 17 '15 at 16:56

3 Answers3

1

From what I understand

  • the (Adr_MSB & 0x3F) part of the takes the last six bits of the Adr_MSB and returns corresponding integer.
    • Eg: 125 (1111101) will return 61 (111101)
    • Note: This step is removing all bits other than last 6 bits, these bits are lost. Hence Lossless inverse function is not possible.
  • The (8 + Adr_LSB) just adds 8 to Adr_LSB.
  • << is a bit wise Left Shift Operator.
    • Eg. 61 << 3 = 488 . Since 61 is 111101, adding three zeros to the right (Left Shifting three times) will give 111101000 which is 488.

Effective inverse of the expression (Adr_MSB & 0x3F) << (8 + Adr_LSB) to be applied to given number x

  1. Take first six bits from x and convert it to int. This will be Adr_MSB.
  2. Count the rest of the bits. Subtract 8 from this count and it will be Adr_LSB.
shanmuga
  • 4,329
  • 2
  • 21
  • 35
0

Does the following represent what you are looking for?

((Adr_MSB & 0x3F) << 8) | Adr_LSB

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
  • No, I need a way to say something like: 1 = (Adr_MSB & 0x3F) << (8 + Adr_LSB) And then calculate both bytes. – grizeldi Aug 17 '15 at 16:27
0

Would this work?

int x = 33;
byte Adr_MSB = (byte)x;
byte Adr_LSB = (byte)24;
System.out.println((Adr_MSB & 0x3F) << (8 + Adr_LSB));

Adr_LSB can also be -8.

Since you do bitwise AND on Adr_MSB and 111111, you have only six consecutive bits available to represent the number, so not all integers can be represented just by shifting those 6 bits. This solution works for x up to enter image description here, so... you can argue that is not a good solution, but it is a start.

Tawcharowsky
  • 615
  • 4
  • 18
  • Tested it out and it works up to 63. For my needs I think that will be more than enough. – grizeldi Aug 17 '15 at 16:44
  • I'll use this for now but if any genious comes up with something better I'll be happy ;) – grizeldi Aug 17 '15 at 16:47
  • You simply defuse the left shift by using 32 (same as supplying zero). Clearly no number having 1-bits separated by more than 7 places can be represented in OP's form. OTOH any shift distance can be achieved by relying on the modulo-32 semantics that Java uses. – Marko Topolnik Aug 17 '15 at 17:12