473,379 Members | 1,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Bit Shifting Question

Is anyone able to help me here? I have the following VB code and wish to
have it rewritten in C++ but unsure how. Any help would be greatly
appreciated. Effectively the code is packing a 10 letter word into 60 bits.
I only need to deal with 64 valid values. ie the values 90 - 64gv so the
first 26 values are not needed. Thanks in advance
Public Function Pack(ByVal TheData As String) As String
Dim c As Double
Dim x As Long
Dim val As Long
Dim bOut(0 To 7) As Byte
Dim lByte As Long
Dim lBit As Long
Dim AscChr As String

TheData = UCase$(TheData) & Space$(10) ' pad to ensure
at least 10 valid chars
For x = 1 To Len(TheData)
val = Asc(Mid$(TheData, x, 1)) - 26 ' Remove lower
26 ascii characters to keep in our range. Highest ascii used is 90(Z) - 26 =
64
If val Then PackBytes bOut, val - 1, lByte, lBit ' needs some
handling for asc values outside valid range A-Z 0-9 & [space]
Next
For x = LBound(bOut) To UBound(bOut)
AscChr = AscChr & Chr(bOut(x))
Next x

CopyMemory c, bOut(0), 8

Pack = AscChr
End Function

Private Sub PackBytes(ByRef TheBits() As Byte, ByVal TheChar As Long, ByRef
CurrentByte As Long, ByRef CurrentBit As Long)
If CurrentByte 7 Or (CurrentByte = 7 And CurrentBit 2) Then Exit Sub
' code too long

If CurrentBit = 0 Then
TheBits(CurrentByte) = TheChar * 4 '
set high 6 bits
CurrentBit = 6
Exit Sub
ElseIf CurrentBit = 2 Then
TheBits(CurrentByte) = TheBits(CurrentByte) Or TheChar '
set low 6 bits
CurrentByte = CurrentByte + 1
ElseIf CurrentBit = 4 Then
TheBits(CurrentByte) = TheBits(CurrentByte) Or (TheChar \ 4) '
set low 2 bits
CurrentByte = CurrentByte + 1
TheBits(CurrentByte) = (TheChar And 3) * 64 '
set high 4 bits
ElseIf CurrentBit = 6 Then
TheBits(CurrentByte) = TheBits(CurrentByte) Or (TheChar \ 16) '
set low 4 bits
CurrentByte = CurrentByte + 1
TheBits(CurrentByte) = (TheChar And 15) * 16 '
set high 2 bits
End If
CurrentBit = CurrentBit - 2
End Sub
Apr 27 '07 #1
2 2257
On Apr 27, 9:02 am, "johnno" <joh...@hotmail.comwrote:
Is anyone able to help me here? I have the following VB code and wish to
have it rewritten in C++ but unsure how. Any help would be greatly
appreciated. [snip]
Depends. How much are you offering in compensation?

Seriously, we'll be more than willing to help if you have a specific C+
+ language question (see <http://www.parashift.com/c++-faq-lite/how-to-
post.html#faq-5.9for what sort of questions are on-topic here). The
operators you're probably interested in are &, |, ~, <<, and >>. Check
the appendix of your favorite C++ book for more information, and if
you don't understand something, feel free to ask here.

Cheers! --M

Apr 27 '07 #2
"johnno" <jo****@hotmail.comwrote in message
news:rv*****************@news-server.bigpond.net.au...
Is anyone able to help me here? I have the following VB code and wish to
have it rewritten in C++ but unsure how. Any help would be greatly
appreciated. Effectively the code is packing a 10 letter word into 60
bits. I only need to deal with 64 valid values. ie the values 90 - 64gv so
the first 26 values are not needed. Thanks in advance
Public Function Pack(ByVal TheData As String) As String
std::string Pack( std::string TheData )
Dim c As Double
double c;
Dim x As Long
long x;
Dim val As Long
long val;
etc...

If you don't know C++ how are you going to code this? There is nothing
extradanary about this program. Everything can be done in C++ by just using
C++ syntax instead of VB.
Dim bOut(0 To 7) As Byte
Dim lByte As Long
Dim lBit As Long
Dim AscChr As String

TheData = UCase$(TheData) & Space$(10) ' pad to ensure
at least 10 valid chars
For x = 1 To Len(TheData)
val = Asc(Mid$(TheData, x, 1)) - 26 ' Remove lower
26 ascii characters to keep in our range. Highest ascii used is 90(Z) - 26
= 64
If val Then PackBytes bOut, val - 1, lByte, lBit ' needs some
handling for asc values outside valid range A-Z 0-9 & [space]
Next
For x = LBound(bOut) To UBound(bOut)
AscChr = AscChr & Chr(bOut(x))
Next x

CopyMemory c, bOut(0), 8

Pack = AscChr
End Function

Private Sub PackBytes(ByRef TheBits() As Byte, ByVal TheChar As Long,
ByRef CurrentByte As Long, ByRef CurrentBit As Long)
If CurrentByte 7 Or (CurrentByte = 7 And CurrentBit 2) Then Exit
Sub ' code too long

If CurrentBit = 0 Then
TheBits(CurrentByte) = TheChar * 4 '
set high 6 bits
CurrentBit = 6
Exit Sub
ElseIf CurrentBit = 2 Then
TheBits(CurrentByte) = TheBits(CurrentByte) Or TheChar '
set low 6 bits
CurrentByte = CurrentByte + 1
ElseIf CurrentBit = 4 Then
TheBits(CurrentByte) = TheBits(CurrentByte) Or (TheChar \ 4) '
set low 2 bits
CurrentByte = CurrentByte + 1
TheBits(CurrentByte) = (TheChar And 3) * 64 '
set high 4 bits
ElseIf CurrentBit = 6 Then
TheBits(CurrentByte) = TheBits(CurrentByte) Or (TheChar \ 16) '
set low 4 bits
CurrentByte = CurrentByte + 1
TheBits(CurrentByte) = (TheChar And 15) * 16 '
set high 2 bits
End If
CurrentBit = CurrentBit - 2
End Sub

Apr 27 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: David Stockwell | last post by:
Hi, My background is c/c++ and java. I'm learning python at this point. My question is does python share java's peculiar mode of bit shifting, or does python adhere closer to c's bit shifting?...
9
by: GGG | last post by:
Noticed something odd in the way bit shifting was working today. As far as I have ever heard, shifting will shift in zeros(signed ints aside) However I foudn something odd when I am shifting...
8
by: ben | last post by:
i have a bit of code, that works absolutely fine as is, but seems over complicated/long winded. is there anyway to shorten/simplify it? the code is below. description of it: it's like strcpy in...
2
by: salsipius | last post by:
Can someone please help me clarify the below code. I think the shifting has to do with converting datatypes and/or loss of data but am not really clear on the details, could you help shed some...
22
by: Sekhar | last post by:
can anyone shed some light on "Bit Shifting" of structures? I have a doubt whether i am using the right key word or not.
10
by: krunalb | last post by:
Hi, I am trying to shift unsigned long long value by 64 bits and this is what i get #include <stdio.h> int main() { unsigned short shiftby= 64;
20
by: Charles Sullivan | last post by:
I understand different processor hardware may store the bits in a byte in different order. Does it make a difference in C insofar as bit-shifting unsigned char variables is concerned? E.g, if I...
16
by: lak | last post by:
i know left and right shift normally,but i cant know what happens if it is negative. for example int x=-2; x<<=1;//what happens here
4
by: Neil | last post by:
I previously posted about data shifting between records in my Access 2000 MDB with a SQL Server 7 back end, using ODBC linked tables. Every once in a while, data from one record mysteriously...
12
by: Boltar | last post by:
I seem to be having yet more wierd issue with bit shifting. It seems the following code doesnt do anything under gcc (ie it returns -1 as both results). Anyone know why? Is it another language...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.