Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# Bit Shifter

Posted on 2011-09-23
8
Medium Priority
?
340 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

604 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