Solved

C# Bit Shifter

Posted on 2011-09-23
8
336 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
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

707 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