bv2 stores the value as 00110001001100100000101000000000
//bv2 is initialized as
bv2 = BitVector( intVal = 0, size = 32 )
//then some bit operation is done
bv2=bv1^bv2
hex(int(bv2,2))
this is giving me error.However if I directly use hex(int('00110001001100100000101000000000',2)) it gives me hexadecimal result.
What is wrong here?
The base argument is used only for strings or bytes. BitVector has a proper __int__() method.
hex(int(bv2))
Related
A file which contains the buffer value. The first 16 bits contain the type. The next 32 bits gives the length of the data. The remaining value in the data.
How can I find the type from the 16 bits (find if it is int or char...)
I'm super stuck in my though process here. Not able to find a way to convert bits to types.
Say you have the homework assignment:
You are given a file where the first bit encodes the type, the
next 7 bits encode the length, and the rest is the data.
The types are encoded in the following way:
0 is for int
1 is for char
Print the ints or chars separated by newlines.
You just use the given information! Since 1 bit is used to encode the type there are two possible types. So you just read the first bit, then do:
if (bit == 0) {
int *i = ...
}
else if (bit == 1) {
char *c = ...
}
Am trying to decode ASN1 format data. As per the Data specification Document. Am expected to get 4 bytes and i need to convert the same to Integer.
def decode_chargingId(self,field_length,field_value):
print(field_lenght)
return int(field_value)
Above is the piece of code being executed and i get below error for the same.
invalid literal for int() with base 10: '\x16'
When i try to see the bytes with break point in VS code debuggers i see the bytes as b'\x16\x00\xf70' .
Am just trying to get the integer value equivalent for those bytes using int function in python.
I was sing wrong method to convert to integer. Below code worked fine to convert bytes to integer.
def decode_chargingId(self,field_length,field_value):
result = 0
for b in field_value:
result = result * 256 + int(b)
return result
I am attempting to convert base 16 to base 36. I'm taking md5 hashes and making them have all 0-9a-z.
I searched around and didn't find anything good. Any suggestions for converting hexadecimal to hexatridecimal in c++? Do you guys know any good libraries for doing it?
I assume the tricky part you're struggling with is the conversion to Rad36, not getting a integral value from a hex number represented as a string. So, here is a function which takes an unsigned __int64, converts it to Radix 36, and returns a string with the converted value.
string rad36(unsigned __int64 v)
{
string retval;
while( v > 0 )
{
unsigned m = v%36;
if( m <= 9 )
retval.insert(0,1,'0'+m);
else
retval.insert(0,1,'A'+m-10);
v /= 36;
}
return retval;
}
The basic solution is to convert your 128-bit number to a (large) integer, then subsequently perform modulus and divide operations by 36.
However, if you're OK with wasting a couple of bits, why not convert to base 32 to make things easier? Then you can do everything with shifting and masking.
In other words does this work as expected?
int32 i = INT_MAX-1;
int64 j = i * i;
or do I need to cast the i to 64 bit first?
You need to cast at least one of the operands to the multiply. At the point the multiply is being done, the system doesn't know you're planning to assign to an int64.
(Unless int64 is actually the native int type for your particular system, which seems unlikely)
It depends on what int32 and int64 are.
In brief, all integers are promoted to at least 'int' size (which may be 64 bits) before any arithmetic operations, and to the size of the larger operand for binary operators if this is of greater rank than an int.
How the result of an expression is used (whether or not it is stored to a wider type) has no bearing on the promotions of the constituent parts of the expression.
The basic answer is no it will not do what you want.
But it does do what is expected.
Two things to note about mathematical operations:
Both operands will be the same type.
The resulting type will be the same as the input.
If the compiler notes a mismatch between the operands it will convert one of the operands so that both match (see Which variables should I typecast when doing math operations in C/C++?). Note: This is done in isolation to what happens to the result.
Given two numbers a,b and each number uses len_a and len_b bits.
Your output datatype needs at least: len_a and len_b bits.
In your above code, you have two 31 bit numbers ( because INT_MAX - 1 = 0x7FFFFFFE uses 31 bits ) and you will need to typecast one of them to int64_t because it will do a 32 bit multiply and overflow before it casts to int64_t.
The number of bits needed for fixed point multiplication:
len_output = howManyBits( a * b )
= len_a + len_b
A quick example to show the above rule in action:
a = 7
len_a = 3
b = 7
len_b = 3
c = a * b
= 49 ( decimal )
= 0x31 ( hex )
len_c = howManyBits( 0x31 )
= 6
You can write a function to count bits. Or if you just want a quick sanity check to confirm this use something like Windows Calc that will convert the number into binary form and count the bits used.
See: 14. Mixed use of simple integer types and memsize types.
http://www.viva64.com/art-1-2-599168895.html
If I have two things which are hex, can I someone how append their binary together to get a value?
In C++,
say I have
unsigned char t = 0xc2; // 11000010
unsigned char q = 0xa3; // 10100011
What I want is somehow,
1100001010100011, is this possible using bit-wise operators?
I want to extract the binary form of t and q and append them...
Yes it's possible.
Just use the left-bitshift operator, shifting to the left by 8, using at least a 16-bit integer. Then binary OR the 2nd value to the integer.
unsigned char t = 0xc2; // 11000010
unsigned char q = 0xa3; // 10100011
unsigned short s = (((unsigned short)t)<<8) | q; //// 11000010 10100011
Alternatively putting both values in a union containing 2 chars (careful of big endian or small) would have the same bit level result. Another option is a char[2].
Concatenating two chars:
unsigned char t = 0xc2; // 11000010
unsigned char q = 0xa3; // 10100011
int result = t; // Put into object that can hold the fully concatenated data;
result <<= 8; // Shift it left
result |= q; // Or the bottom bits into place;
Your example doesn't really work too well because the width (usually 8-bits) of the input values aren't defined. For example, why isn't your example: 0000000100000010, which would be truly appending 1 (00000001) and 2 (00000010) bit wise.
If each value does have a fixed width then it can be answered with bit shifting and ORing values
EDIT: if your "width" is defined the full width with all leading zero's removed, then it is possible to do with shifting and ORing, but more complicated.
I'd go with the char array.
unsigned short s;
char * sPtr = &s;
sPtr[0] = t; sPtr[1] = q;
This doesn't really care about endian..
I'm not sure why you'd want to do this but this would work.
The problem with the bit methods are that you're not sure what size you've got.
If you know the size.. I'd go with Brians answer
There is no append in binary/hex because you are dealing with Numbers (can you append 1 and 2 and not confuse the resulting 12 with the "real" 12?)
You could delimit them with some special symbol, but you can't just "concatenate" them.
Appending as an operation doesn't really make sense for numbers, regardless of what base they're in. Using . as the concatenation operator: in your example, 0x1 . 0x2 becomes 0x12 if you concat the hex, and 0b101 if you concat the binary. But 0x12 and 0b101 aren't the same value (in base 10, they're 18 and 5 respectively). In general, A O B (where A and B are numbers and O is an operator) should result in the same value no matter what base you're operating in.