Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7845887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:23:47+00:00 2026-06-02T17:23:47+00:00

What am I doing: I add four integers in C. on the way, I

  • 0

What am I doing:
I add four integers in C. on the way, I lose information.

See code below:

//c-file
jbyte *inputByteArray = (*env)->GetDirectBufferAddress (env, obj);

// checked every value, also sizeof(val1)= 4 etc...
int val1 = (int) *(inputByteArray + 1); //120 
int val2 = (int) *(inputByteArray + 2); //120
int val3 = (int) *(inputByteArray + 3); //180
int val4 = (int) *(inputByteArray + 4); //180
int result = val1 + val2 + val3 + val4;

return result; 
//return type is int

//output: 88, should be 600
// 88 binary: 0000 0000 0101 1000
//600 binary: 0000 0010 0101 1000

The special thing about this is the following, which might be causing the problem:

The 4 values for the input are from a handed-over Buffer from Java, which is a direct ByteBuffer. It is directly allocated in Java, in order NOT be moved by the garbage collector. On the c-side I hand the buffer over via pointer from “GetDirectBufferAddress” (see code), and the single values do match the values in the array.

Does anyone know about this strange behaviour?

When I am using IntBuffer to hand over the numbers, it works by the way.
Im working here on performance, so I want small buffers and my data values are small enough to use ByteBuffer. (this is only a fragment of a larger calculation on the c-code side)

Since this is on Android, I did not manage to debug into the c-code…

Edit: I am using eclipse/SDK/NDK in current versions on Android 3.2.1 testing device

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:23:49+00:00Added an answer on June 2, 2026 at 5:23 pm

    As @Deucalion says your array looks dodgy. Unless you are trying to add array1, array[2], array[3] and array[4]. Without using array[0].
    Anyway assuming that what you have done is what you intend, your value is exactly what you will get.
    Byte range is -128 t0 +127. And so 180 is actually stored as -76. And voila !!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Doing the below will reproduce my problem: New WPF Project Add ListView Name the
I am adding a button into UITableView by doing the following // Add checkOut
I'm doing these steps: Create new vs2010 C# console application Project menu -> Add
I know you can’t add a class called Socket in VS2010 by doing: Right
Can I add new method to aspxclienttextbox control? I'm doing custom control on the
hello i'm learning qt and i'm doing the folowing to add some widgets to
I'm doing a simple slideUp animation on an object. I want to add attribute
can you tell me how this line works.... my OperatorFactory.get(add) is not doing anything.
Doing an ajax get request works as expected using the following code: $.ajax({ type:
I'm trying to add a group of four radio buttons to a form. There

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.