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 6840197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:48:46+00:00 2026-05-26T23:48:46+00:00

I have a two’s complement representation of a number, in a byte array, and

  • 0

I have a two’s complement representation of a number, in a byte array, and I want to expand it to use a larger byte array.
(You can get two’s complement byte[] out of BigIntegers)

So my new byte array must have the same sign bit (ie highest bit)
and then everything else shifted.

so:

byte[] resize (byte[] raw, int len)
{
byte high = raw[0];

resized[0] = high & 0x80 //ie binary 1000 0000
raw[0] = high & 0x7F // 0111 1111
//causes a side effect but i don't care raw is a throw away value

byte[] resized = new byte[len];
    system.ArrayCopy(raw,0,resized,len-raw.length, len);
}

Am I on the right track?
I’m having trouble getting my head around, byte length hex literals work.
Or do my hex literals have to be int sized?
If so I’m going to have to cast my everything.

so is:

resized[0] = (byte) ((int)high & 0x8000)
raw[0] = (byte)((int)high & 0x7FFF)

The equivalent?

  • 1 1 Answer
  • 1 View
  • 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-05-26T23:48:47+00:00Added an answer on May 26, 2026 at 11:48 pm

    Step 1: save the sign bit (i.e: the Most Significant bit)
    Step 2: the new padding bits should be the same as the sign bit. Example:

    1 byte: 11110001 
    2 bytes: 1111111111110001
    8 bytes: 1111111111111111111111111111111111111111111111111111111111110001
    

    for positive values is the same:

    1 byte: 00001111
    2 bytes: 0000000000001111
    8 bytes: 0000000000000000000000000000000000000000000000000000000000001111
    

    Notice that when performing bitwise operations, the bytes are promoted to ints. Padding bytes will thus be 00h or FFh depending on the sign. The rest of the bytes are the same as the originals.

    The sign can be determined as follows:

    //If the MSB were raw[0]:
    boolean isNegative = false;
    if(raw[0] & 0x80 != 0){
          isNegative = true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays, I want to merge these two arrays into single array.
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two classes, and want to include a static instance of one class
I have two spreadsheets... when one gets modified in a certain way I want
I have two webapplication, one is a simple authenticationsite which can authenticate the logged
I have two classes Order and Items I want a method like this class
I have two inputs with the same date, but i want to separate the
I have two different Classes: class X { public int TX1 { get; set;
I have two tables, say teacher and student. I want to build a third

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.