Ok, here's the function I'm a bit mystified by(Little rusty bitwise operators)
void two_one(unsigned char *in,int in_len,unsigned char *out)
{
unsigned char tmpc;
int i;
for(i=0;i<in_len;i+=2){
tmpc=in[i];
tmpc=toupper(tmpc);
if((tmpc<'0'||tmpc>'9') && (tmpc<'A'||tmpc>'F'))tmpc='F';
if(tmpc>'9')
tmpc=toupper(tmpc)-'A'+0x0A;
else
tmpc-='0';
tmpc<<=4; //Confused here
out[i/2]=tmpc;
tmpc=in[i+1];
tmpc=toupper(tmpc);
if((tmpc<'0'||tmpc>'9') && (tmpc<'A'||tmpc>'F'))tmpc='F';
if(tmpc>'9')
tmpc=toupper(tmpc)-'A'+0x0A;
else
tmpc-='0';
out[i/2]|=tmpc; //Confused Here
}
}
I marked the two places that I don't quite understand.
If anyone could help me convert those pieces to Vb.Net, or at least help me understand what's going on there, that'd be awesome.
Thanks.
Update
So this is what I came up with, but it's not quite giving me back the right data...Anything look wrong here?
Public Function TwoOne(ByVal inp As String) As String
Dim temp As New StringBuilder()
Dim tempc As Char
Dim tempi As Byte
Dim i As Integer
Dim len = inp.Length
inp = inp + Chr(0)
For i = 0 To len Step 2
If (i = len) Then Exit For
tempc = Char.ToUpper(inp(i))
If ((tempc < "0"c Or tempc > "9"c) AndAlso (tempc < "A"c Or tempc > "F"c)) Then
tempc = "F"c
End If
If (tempc > "9"c) Then
tempc = Char.ToUpper(tempc)
tempc = Chr(Asc(tempc) - Asc("A"c) + &HA)
Else
tempc = Chr(Asc(tempc) - Asc("0"c))
End If
tempc = Chr(CByte(Val(tempc)) << 4)
Dim tempcA = tempc
tempc = Char.ToUpper(inp(i + 1))
If ((tempc < "0"c Or tempc > "9"c) AndAlso (tempc < "A"c Or tempc > "F"c)) Then
tempc = "F"c
End If
If (tempc > "9"c) Then
tempc = Char.ToUpper(tempc)
tempc = Chr(Asc(tempc) - Asc("A"c) + &HA)
Else
tempc = Chr(Asc(tempc) - Asc("0"c))
End If
temp.Append(Chr(Asc(tempcA) Or Asc(tempc)))
Next
TwoOne = temp.ToString()
End Function
tmpc <<= 4 shifts the bits in tmpc 4 places to the left, then assigns the value back to tmpc. Hence if tmpc was 00001101, it becomes 11010000
out[i/2]|=tmpc bitwise-ors the array value with tmpc. Hence if out[i/2] is 01001001 and tmpc is 10011010, then out[i/2] becomes 11011011
EDIT (updated question):
The lines tmpc-='0'; in the original are not exactly the same as your new code tempc = "0"c. -= subtracts the value from the variable, and hence you need tempc = tempc - "0"c or similar
tmpc<<=4; (or tmpc = tmpc << 4;) shifts tmpc left by 4 bits.
out[i/2]|=tmpc; (or out[i/2] = out[i/2] | tmpc;) bitwise-ORs out[i/2] with tmpc.
Related
I am trying to add a number to a pointer value with the following expression:
&AddressHelper::getInstance().GetBaseAddress() + 0x39EA0;
The value for the &AddressHelper::getInstance().GetBaseAddress() is always 0x00007ff851cd3c68 {140700810412032}
should I not get 0x00007ff851cd3c68 + 0x39EA0 = 7FF81350DB08 as a result?
while I am getting: 0x00007ff851ea3168 or sometimes 0x00007ff852933168 or some other numbers.
Did I took the pointer value incorrectly?
With pointer arithmetic, type is taken into account,
so with:
int buffer[42];
char* start_c = reinterpret_cast<char*>(buffer);
int *start_i = buffer;
we have
start_i + 1 == &buffer[1]
reinterpret_cast<char*>(start_i + 1) == start_c + sizeof(int).
and (when sizeof(int) != 1) reinterpret_cast<char*>(start_i + 1) != start_c + 1
In your case:
0x00007ff851ea3168 - 0x00007ff851cd3c68) / 0x39EA0 = 0x08
and sizeof(DWORD) == 8.
In a binary representation of a number is there a simpler way than this
long half(long patten, bool exclusive) {
if (patten == 0) {
return 1;
}
long newPatten = 0;
for (int p = 0; p < MAX_STEPS; p++) {
long check = 1 << p;
if ((check & patten) > 0) {
int end = (p + MAX_STEPS) - 1;
for (int to = p+1; to <= end; to++) {
long checkTo = 1 << (to % MAX_STEPS);
if ( (checkTo & patten) > 0 || to == end ) {
int distance = to - p;
long fullShift = (int)round( ((float)p) + ((float)distance)/2 );
long shift = fullShift % MAX_STEPS;
long toAdd = 1 << shift;
newPatten = newPatten | toAdd;
break;
}
}
}
}
return exclusive ? patten ^ newPatten : patten | newPatten;
}
To add a bit in the middle of any two other bits with wrapping around and rounding to one side when there is an even number of positions between?
E.g.
010001 to
110101
Or
1001 to
1101
Update: These bits aren't coming from anywhere else, such as another number, just want a new True bit to be in the middle of any other two True bits, so if there was a 101, then the middle would be the 0 and the result would be 111. Or if we started with 10001, then the middle would be the center 0 and the result would be 10101.
When i say wrapping i also mean adding bits as if the bit array was a circle so if we had 00100010 representing the positions with letters:
00100010
hgfedcba
So we have True bits at b and f we would put a middle bit between b and f going left to right at d but also going right to left, wrapping around and putting it at h resulting in:
10101010
hgfedcba
I understand this isn't a usual problem.
Are there known tricks to do things like this without loops?
I have a problem with verilog if-else statement. I'm trying to make a digital clock and my tick_counter module code like this. I pointed to error line with comment line. I cant find the solve, please help me.
module tick_counter(
input clk,
input tick_in,
output [3:0] ssd_2, ssd_4,
output [2:0] ssd_3
);
reg [5:0] count1, count_next1;
reg [2:0] count2, count_next2;
reg [3:0] count3, count4, count_next3, count_next4;
always#(posedge clk)
begin
count1 <= count_next1;
count2 <= count_next2;
count3 <= count_next3;
count4 <= count_next4;
end
//next state logic
always#*
begin
if(tick_in)
begin
if(count1==6'b111100) //second counter
begin
count_next1 = 6'b0;
count_next2 = count2 + 1'b1;
end
else
count_next1 = count1 + 1'b1;
if(count2==4'b1001) //minutes counter of LSB digit
begin
count_next2 = 4'b0000;
count_next3 = count3 + 1'b1;
end
else
count_next2 = count2 + 1'b1;
if(count3==3'b101) //minutes counter of MSB digit
begin
count_next3 = 3'b000;
count_next4 = count4 + 1'b1;
end
else
count_next3 = count3 + 1'b1;
if(count4==4'b1001) //counter hour
begin
count_next4 = 4'b0000;
end
else
count_next4 = count4 + 1'b1;
else //---THE POINT OF ERROR------
begin
count_next1 = count1;
count_next2 = count2;
count_next3 = count3;
count_next4 = count4;
end
end
end
assign ssd_2 = count2;
assign ssd_3 = count3;
assign ssd_4 = count4;
endmodule
Of course endmodule is missing. But this may be your typo mistake.
Proper indentation is greatly encouraged for large designs. Here, after indenting your code, I came to know that one end after the else part of // counter hour condition is missing. So, your main if condition's end is mis-placed. Just add end at the specified position and remove one end from the last of always block.
Kindly do proper indentation so that it becomes easy to debug.
I am trying to make a simple Arduino code for when a photocell reading is less than 900, it will add 1 to the CurrentNumber and display it on a 4 digit 7 segment display. The problem is that it wont stop adding one even though it's reading more than 1000.
void loop() {
photocellReading = analogRead(photocellPin);
Serial.print("Analog reading = ");
Serial.println(photocellReading); // the raw analog reading
photocellReading = 1023 - photocellReading;
if(photocellReading << 10){
CurrentNumber = CurrentNumber + 1;
}
displayNumber(CurrentNumber);
}
Your problem is in your if condition:
if(photocellReading << 10){
CurrentNumber = CurrentNumber + 1;
}
What you're essentially doing he is: shifting the bits of photocellReading to the left by 10 (which is equivalent to multiplying by 2^10 aka 1024). Most likely this means the only time this is ever going to be false is if the value of photocellReading was 0 to start with. (I say most likely because it depends on if the bits loop back around or not, but this is not entirely relevant).
tl;dr your code is conceptually equivalent to:
if((photocellReading * 1024) != 0){
CurrentNumber = CurrentNumber + 1;
}
What I'm guessing you wanted to do (considering you subtracted 1023, which coincidentally is 1024 - 1) is:
if(photocellReading < 1024){ // again 1024 == 2^10
CurrentNumber = CurrentNumber + 1;
}
I really do not understand what is happening here but:
when I do:
colorIndex += len - stopPos;
for(int m = 0; m < len - stopPos; m++)
{
colorUniPos++;
}
it does not give me the same result as doing:
colorIndex += len - stopPos;
colorUniPos += len - stopPos;
I think it becomes off by one or something. Shouldn't both of these obtain the same result?
Thanks
This won't produce the same result if len - stopPos < 0
This is correct if len - stopPos is a positive value or zero, but for negative values colorUniPos just keeps its value because the loop isn't executed.