B4J Question Bit.shiftright 32 bit limitation or Bug?

MbedAndroid

Active Member
Licensed User
Longtime User
following code:
B4X:
    Dim nodeword=0 As Long
   For x=1 To 32
       nodeword=Bit.Shiftright(nodeword,1)
       If nodevalues(x).Status>0 Then
           nodeword=Bit.Or(nodeword,0x80000000)
           Log(nodevalues(x).Name & " 1-" & "nw:"& nodeword)
       Else
           Log (nodevalues(x).Name & " 0-"& "nw:" & nodeword)
       End If
       
   Next
if i try to shift 28 bits, it works, but doesnt take the last 4 positions as i shift 28
if i try to shift 32 bits, the result of nodeword is 1, whatever condition found
long is 8 bytes, so it should work.
is this a bug in b4j? or limitation of shift?

result of logdump
at x=8 the result is 8388608, 1 bit missing, not inserted (at x=1 a '1' is inserted, at x=8 a ''1'' is inserted, at x=11 a "1' is inserted, but those are not shifted in anymore

update Cangroup
inserted:1-nw:-2147483648
inserted: 0-nw:-1073741824
inserted: 0-nw:-536870912
inserted: 0-nw:-268435456
inserted: 0-nw:-134217728
inserted: 0-nw:-67108864
inserted: 0-nw:-33554432
inserted: 0-nw:-16777216
inserted:1-nw:-8388608
inserted: 0-nw:-4194304
inserted: 0-nw:-2097152
inserted:1-nw:-1048576
inserted: 0-nw:-524288
inserted:1-nw:-262144
inserted:1-nw:-131072
inserted: 0-nw:-65536
inserted: 0-nw:-32768
inserted: 0-nw:-16384
inserted: 0-nw:-8192
inserted: 0-nw:-4096
inserted: 0-nw:-2048
inserted: 0-nw:-1024
inserted: 0-nw:-512
inserted: 0-nw:-256
inserted: 0-nw:-128
inserted: 0-nw:-64
inserted: 0-nw:-32
inserted: 0-nw:-16
inserted: 0-nw:-8
inserted: 0-nw:-4
inserted: 0-nw:-2
inserted: 0-nw:-1

same when i inserted all "1'"

inserted:1-nw:-2147483648
inserted:1-nw:-1073741824
inserted:1-nw:-536870912
inserted:1-nw:-268435456
inserted:1-nw:-134217728
inserted:1-nw:-67108864
inserted:1-nw:-33554432
inserted:1-nw:-16777216
inserted:1-nw:-8388608
inserted:1-nw:-4194304
inserted:1-nw:-2097152
inserted:1-nw:-1048576
inserted:1-nw:-524288
inserted:1-nw:-262144
inserted:1-nw:-131072
inserted:1-nw:-65536
inserted:1-nw:-32768
inserted:1-nw:-16384
inserted:1-nw:-8192
inserted:1-nw:-4096
inserted:1-nw:-2048
inserted:1-nw:-1024
inserted:1-nw:-512
inserted:1-nw:-256
inserted:1-nw:-128
inserted:1-nw:-64
inserted:1-nw:-32
inserted:1-nw:-16
inserted:1-nw:-8
inserted:1-nw:-4
inserted:1-nw:-2
inserted:1-nw:-1
 

Daestrum

Expert
Licensed User
Longtime User
Bit.ShiftRight will only accept up to 31 as the shift as it is working on an Integer value (the long you supplied will be cast to an Integer)
If you want true 64bit shiftright you need to code your own
eg
B4X:
public static Long longShift(Long val,Integer shft){
 return val >> shft;
}
shft can be 0 to 63 places now, as it is really working on a Long.
 
Upvote 0

MbedAndroid

Active Member
Licensed User
Longtime User
yep, i see. a int is 4 bytes, and the last bit is a signbit
The problem is in both functions OR and Shift.
I replaced it by shift =/2 and OR just adding 0x80000000
This works in the example
too bad java doesnt know unsigned int's
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
too bad java doesnt know unsigned int's

B4J test code:
B4X:
Dim jo As JavaObject
jo = Me
Log(-4 >> 1)
Log(jo.RunMethod("ShiftRight",Array(-4,1)))
Dim a As Long = -4
Log(jo.RunMethod("ShiftRight2",Array(a,1)))

Inline Java code:
B4X:
#If JAVA
public static int ShiftRight(int a, int b) {
   return a >>> b;
}

public static long ShiftRight2(long a, int b) {
   return a >>> b;
}
#End If
 
Upvote 0

MbedAndroid

Active Member
Licensed User
Longtime User
java.lang.RuntimeException: java.lang.RuntimeException: Method: ShiftRight not found in: b4j.example.main

reflextion lib is inserted
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
On Android you have to say jo.InitializeContext instead of jo=Me. In B4J, this uses the JavaObject library.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

MbedAndroid

Active Member
Licensed User
Longtime User
solved this in a very nice way. Splitted the long in 4 bytes, and processed each byte. It's not a caluculation, but a frame send to a Candevice
Anyway thanks for the tip, next time may be!
 
Upvote 0
Top