Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Bitwise Operation Issues  (Read 317 times)
0 Members and 1 Guest are viewing this topic.
Offline Dane

Senior Newbie





« Posted 2013-02-17 11:27:01 »

I can't figure out why this isn't writing/reading properly.

Basically what I'm trying to do is write numbers to an array of bytes, and then read them. It's pretty self-explanatory if you take a look.

Any help would be greatly appreciated.


Test.java:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
import org.valhalla.io.Payload;

public class Test
{

   public Test()
   {
      try {
         Payload p = new Payload(32);
         p.putLong(0x7fffffffffffffffL);
         p.putInt(0xFFFFFF);
         p.putShort(0xFFFF);
         p.put(0xFF);
         p.putFloat(1.234f);
         
         p.reset(); // Resets the position back to zero.
       
         System.out.println("Value: " + p.getLong() + ", " + p.getInt() + ", " + p.getShort() + ", " + p.get() + ", " + p.getFloat());
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   public static void main(String[] args) {
      new Test();
   }

}


Output:
1  
Value: -1, 16777215, 65535, 255, 1.234


Payload.java:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
package org.valhalla.io;

import java.io.IOException;
import java.io.OutputStream;

public class Payload
{

   private byte[] data;
   private int position = 0;
   private OutputStream out;

   public Payload(int size)
   {
      this.data = new byte[size];
   }

   public Payload(OutputStream out, int size)
   {
      this.out = out;
      this.data = new byte[size];
   }

   public Payload(byte[] data)
   {
      this.data = data;
   }

   public int get() {
      return (data[position++] & 0xFF);
   }

   public Payload put(int i) {
      this.data[position++] = (byte) (i & 0xFF);
      return this;
   }

   public boolean getBoolean() {
      return get() == 1;
   }

   public Payload putBoolean(boolean flag) {
      this.put(flag ? 1 : 0);
      return this;
   }

   public int getShort() {
      return get() << 8 | get();
   }

   public Payload putShort(int i) {
      put(i >>> 8);
      put(i);
      return this;
   }

   public int getInt() {
      return getShort() << 16 | getShort();
   }

   public Payload putInt(int i) {
      putShort(i >>> 16);
      putShort(i);
      return this;
   }

   public long getLong() {
      return getInt() << 32L | getInt();
   }

   public Payload putLong(long l) {
      putInt((int) (l >>> 32L));
      putInt((int) l);
      return this;
   }

   public double getDouble() {
      return Double.longBitsToDouble(getLong());
   }

   public Payload putDouble(double d) {
      this.putLong(Double.doubleToLongBits(d));
      return this;
   }

   public float getFloat() {
      return Float.intBitsToFloat(getInt());
   }

   public Payload putFloat(float f) {
      this.putInt(Float.floatToIntBits(f));
      return this;
   }

   public Payload reset() {
      this.position = 0;
      return this;
   }

   public Payload clean() {
      for (int i = 0; i < this.data.length; i++) {
         this.data[i] = 0;
      }
      return this;
   }

   public Payload flush() throws IOException {
      if (this.getOutputStream() == null) {
         System.out.println("Warning: Attempted to flush payload with no output stream!");
         return this;
      }
      out.write(this.data);
      return this.clean().reset();
   }

   public Payload setData(byte[] data) {
      this.data = data;
      return this;
   }

   public final byte[] getData() {
      return this.data;
   }

   public Payload setOutputStream(OutputStream out) {
      this.out = out;
      return this;
   }

   public OutputStream getOutputStream() {
      return this.out;
   }

}

Doing it wrong since 0x7CC.
Offline cylab

JGO Knight


Medals: 34



« Reply #1 - Posted 2013-02-17 12:21:55 »

You clean your payload on every get, since the Pair constructor calls clean() on it...

1  
2  
3  
4  
5  
6  
      public Pair(Payload payload, T value)
      {
         this.payload = payload;
         this.value = value;
        this.payload.clean().reset();
      }


Unoffensive tip: learn how stepping and variable inspection work in your IDEs debugger Wink

Btw. this code produces a lot of garbage (memory management wise), since you effectively creating multiple temporary Pair objects on every get call. Better refactor the gets to some equivalents of your put mechanism.

Mathias - I Know What [you] Did Last Summer!
Offline Dane

Senior Newbie





« Reply #2 - Posted 2013-02-17 12:39:01 »

You clean your payload on every get, since the Pair constructor calls clean() on it...

1  
2  
3  
4  
5  
6  
      public Pair(Payload payload, T value)
      {
         this.payload = payload;
         this.value = value;
        this.payload.clean().reset();
      }


Unoffensive tip: learn how stepping and variable inspection work in your IDEs debugger Wink

Btw. this code produces a lot of garbage (memory management wise), since you effectively creating multiple temporary Pair objects on every get call. Better refactor the gets to some equivalents of your put mechanism.

Thanks for this, I don't know what I was thinking when I put that in the constructor. Completely my fault for working on this at 3AM in the morning.

Also I hope I refactored properly, I've edited the original post with the updated source. Smiley


Edit: Also it appears getDouble or putDouble may not be working properly.

1  
2  
3  
4  
5  
6  
7  
8  
         Payload p = new Payload(32);
         p.putDouble(123.10d);
         p.putFloat(123.10f);
         p.putLong(0x73FD73FDL);

         p.reset();

         System.out.println("Double: " + p.getDouble() + ", Float: " + p.getFloat() + ", Long: " + Long.toHexString(p.getLong()));


Output:
1  
Double: 8.49591604E-315, Float: 123.1, Long: 73fd73fd

Doing it wrong since 0x7CC.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (22 views)
2014-04-15 18:08:23

BurntPizza (17 views)
2014-04-15 03:46:01

UprightPath (31 views)
2014-04-14 17:39:50

UprightPath (15 views)
2014-04-14 17:35:47

Porlus (31 views)
2014-04-14 15:48:38

tom_mai78101 (57 views)
2014-04-10 04:04:31

BurntPizza (114 views)
2014-04-08 23:06:04

tom_mai78101 (214 views)
2014-04-05 13:34:39

trollwarrior1 (182 views)
2014-04-04 12:06:45

CJLetsGame (189 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!