Solved

C# Bit Shifter

Posted on 2011-09-23
8
335 Views
Last Modified: 2013-12-17
How do you modify the Shifter method to move the bits to the appropriate locations?

Thx
using System;
using System.Diagnostics;

class Bitshifter
{
    // Bits   Description
    // 31:16  a
    // 15:8   b
    // 7:4    must be zero
    // 3:0    c
    // +----------------+--------+----+----+
    // |        a       |    b   | mbz| c  |
    // +----------------+--------+----+----+
    //  3              1 1
    //  1              6 5      8 7  4 3  0

    // Assembles a, b, and c into the single 32-bit register described above
    static UInt32 shifter(UInt32 a, UInt32 b, UInt32 c)
    {
        return 0;
    }

    static void Main(string[] args)
    {
        Debug.Assert(0x101070e == shifter(0x101, 7, 0xe));
        //Debug.Assert( 0x8101070e == shifter( 0x8101, 0xff07, 0xe ) );
        Console.WriteLine("Success!");
    }

}

Open in new window

0
Comment
Question by:CipherIS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 15

Expert Comment

by:x77
ID: 36592087
  Basic Solution:   return (((A << 15) | (B << 8)) | C);

But note that you need do Validations before:

    if (A > 0x10000)        throw new Exception("A Overflow");
    if (B > 0x100)            throw new Exception("b Overflow");
    if (C > 0x10)              throw new Exception("c Overflow");
    return (((A << 16) | (B << 8)) | C);
0
 
LVL 15

Expert Comment

by:x77
ID: 36592090
Sorry, validation  comparison must be  ">="
0
 
LVL 1

Author Comment

by:CipherIS
ID: 36592577
That is not the solution.  Please look at this - this may help.

    //Shift:
    //x = 1;
    //x = x << 1; // x is now 2  (00000010 binary, 0x02 hex)
    //x = x << 4; // x is now 32 (00100000 binary, 0x20 hex)

    //Bitwise OR:
    //00 | 00 = 00
    //00 | 01 = 01
    //11 | 10 = 11
    //11 | 11 = 11

    //Bitwise AND:
    //00 & 00 = 00
    //00 & 01 = 00
    //11 & 10 = 10
    //11 & 11 = 11
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36593994
Is this schoolwork?
0
 
LVL 15

Expert Comment

by:x77
ID: 36594055
About Shift operator.

There two Sift operator : Signed, Unsigned

When you wants put Bit 0-15 to position 16-31 you can use:  myvar = myVar << 16

On Unsigned, Bit 0-15 = 0
on Signed      Bit 0-15 = Sign bit. - for positive numbers identical to Unsigned.

To Put bit 31-16 to position 15-0:  myVar = myVar >> 16
Same considerations on Bits 31-16 as before on Bit 0-15.

I Put correct solution except first line (15 must be 16) as I put on last line.

As you can see solution is some more complex on signed values as you need aditional mask or cast to forze Unsigned Shift (note that cast can trhow exception when Checked, By default Csharp uses UnChecked).

As You says values and result is unsigned, I don´t put code to mask (and) result from Shift operation.
but I put code to verify Overflow on each bit area.

By sample, for  Signed values:

  Return  ((A << 16) & 0xFFFF0000) | ((b << 8) & 0xFF00) | (C  & 0xF);

On this solution, bits that can overflow are ignored.
0
 
LVL 1

Author Comment

by:CipherIS
ID: 36594215
This is not schoolwork
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 36597162
x77 accidentally typed 15 in his first solution. The actual solution would be his second one:
return (A << 16) | (B << 8) | C;
Which would work if A, B, and C were in the right range. To guarantee that B and C don't mess things up you could do
return (A << 16) | ((B&255) << 8) | (C&15);

This would ensure that no part of B overwrites A and no part of C overwrites the part that must be 0s. If you prefer hex notation (which I do) then use:

return (A << 16) | ((B&0xFF) << 8) | (C&0x0F);
The bits 4-7 that need to be 0s will be 0 from B's shift so it will work no matter what you put in for a, b, and c.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question