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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:43:20+00:00 2026-05-14T04:43:20+00:00

Is there some instruction in MIPS that will determine the parity of a certain

  • 0

Is there some instruction in MIPS that will determine the parity of a certain bit representation? I know to determine whether a “number” has an even parity or an odd parity is to XOR the individual bits of the binary representation together, but that seems computationally-intensive for a set of MIPS instructions… and I need to do this as quick as possible.

Also, the number I’m working in is represented in Grey Code… just to throw that in there. So is there some pseudo-instruction in MIPS to determine the parity of a “number” or do I have to do it by hand?

If there is no MIPS instruction, which it seems very unlikely to be, any advice on how to do it by hand?

Thanks,
Hristo

follow-up: I found a optimization, but my implementation isn’t working.

unsigned int v; // 32-bit word
v ^= v >> 1;
v ^= v >> 2;
v = (v & 0x11111111U) * 0x11111111U;
return (v >> 28) & 1;
  • 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-05-14T04:43:21+00:00Added an answer on May 14, 2026 at 4:43 am

    I’m not aware of any MIPS variant with a parity instruction, but there’s a bit-twiddling trick for calculating parity faster than the obvious method of running through each of the 32 bits in turn. In C:

    result = in ^ (in >> 16);
    result ^= (result >> 8);
    result ^= (result >> 4);
    result ^= (result >> 2);
    result ^= (result >> 1);
    result &= 1;
    
    • After the first step, the bottom 16 bits of the result contain the parity of bits N and N+16 of the input – essentially, 16 steps of the parity calculation have been performed in one go. Writing result{N} to mean “bit N of result“:

      result{0}  =  in{0} ^ in{16}
      result{1}  =  in{1} ^ in{17}
      result{2}  =  in{2} ^ in{18}
      ...
      result{7}  =  in{7} ^ in{23}
      result{8}  =  in{8} ^ in{24}
      ...
      result{15} = in{15} ^ in{31}
      

      (and the remaining top 16 bits of result can now be ignored; they serve no useful purpose in the remainder of the calculation).

    • After the second step, the bottom 8 bits of result contain the parity of bits N, N+8, N+16, N+24 of the original input:

      result{0} = result{0} ^ result{8}  =  in{0} ^  in{8} ^ in{16} ^ in{24}
      result{1} = result{1} ^ result{9}  =  in{1} ^  in{9} ^ in{17} ^ in{25}
      ...
      result{7} = result{7} ^ result{15} =  in{7} ^ in{15} ^ in{23} ^ in{31}
      

      (and again, the remaining bits can be ignored from here onwards).

    • …and so on, until the parity of all the bits of the original input ends up in the bottom bit of result:

      result{0} = in{0} ^ in{1} ^ in{2} ^ ... ^ in{30} ^ in{31}
      

    This is easy to translate directly to MIPS assembly; it’s 11 instructions:

    # input in $a0, output in $v0, $t0 corrupted
    srl $t0, $a0, 16
    xor $v0, $a0, $t0
    srl $t0, $v0, 8
    xor $v0, $v0, $t0
    srl $t0, $v0, 4
    xor $v0, $v0, $t0
    srl $t0, $v0, 2
    xor $v0, $v0, $t0
    srl $t0, $v0, 1
    xor $v0, $v0, $t0
    and $v0, $v0, 1
    

    A possible improvement might be to use a lookup table. For example, after the first two steps, we have:

        result{0} =  in{0} ^  in{8} ^ in{16} ^ in{24}
        result{1} =  in{1} ^  in{9} ^ in{17} ^ in{25}
        ...
        result{7} =  in{7} ^ in{15} ^ in{23} ^ in{31}
    

    so we could use a 256-byte lookup table at this point. In C:

    result = in ^ (in >> 16);
    result ^= (result >> 8);
    result = lookup_table[result & 0xff];
    

    where lookup_table[n] has been precalculated, e.g.:

    for (i = 0; i < 256; i++) {
        n = i ^ (i >> 4);
        n ^= (n >> 2);
        n ^= (n >> 1);
        lookup_table[i] = n & 1;
    }
    

    This is 7 MIPS instructions, not counting loading the lookup table base address into a register:

    # input in $a0, lookup table address in $a1, output in $v0, $t0 corrupted
    srl  $t0, $a0, 16
    xor  $v0, $a0, $t0
    srl  $t0, $v0, 8
    xor  $v0, $v0, $t0
    andi $v0, $v0, 0xff
    addu $t0, $a1, $v0
    lbu  $v0, 0($t0)
    

    However, this is 7 instructions which include a memory access, versus 11 instructions which are purely register operations; it may or may not be faster. (This sort of micro-optimisation always needs to be profiled!)

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

Sidebar

Related Questions

Is there some regex that can be passed to re-seq such that it will
as you can see in jqgrid. There are some parameter that will be send
There are some patterns for checking whether a parameter to a method has been
Take MIPS instruction format described here , there are some abbreviations eg rd,rs and
Are there some good resources tutorials or anyone has tried to implement a Capcha
Is there some kind of software that I can feed uploaded images to and
I am confused with instruction to install ffmpeg-php for video converting... There are some
As we know, one high-level language instruction like counter++ in C++ will be translated
Here is some MIPS assembly code I wrote to test the jump instruction: addi
There are some Assembly instruction in Delphi Source Codes which I cannot found anywhere

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.