How to treat given integer as binary values? [duplicate] - python-3.x

This question already has answers here:
Convert base-2 binary number string to int
(10 answers)
Closed 2 years ago.
I have a integer value i.e. 1010110. How to treat this value as binary? So that i can find the integer value of that binary value.

You can pass a base parameter (2 in this case) to the int function:
s = "1010110"
i = int(s, 2)
# 86

Related

Writing a vector to disk in fortran [duplicate]

This question already has answers here:
Controlling newlines when writing out arrays in Fortran
(1 answer)
How to write matrix row by row with implied do loop? [duplicate]
(1 answer)
Closed 1 year ago.
I am having some issues with writing efficiently a vector in fortran to disk.
Here's a minimal working example:
integer :: ind
double precision, allocatable :: c_reshape
allocate(c_reshape(576000))
do ind = 1,576000
c_reshape(ind) = ind
end do
OPEN(UNIT=25,FILE='c_res',STATUS='replace',ACTION='write')
WRITE(25,*) c_reshape(:)
CLOSE(UNIT=25)
When I open the output file in notepad this is what I see:
Why does Fortran save it in 3 columns? Anyway of saving it in a single column without writing a loop?

unsafe.SizeOf() says any string takes 16 bytes, but how? [duplicate]

This question already has an answer here:
String memory usage in Golang
(1 answer)
Closed 2 years ago.
Simply running fmt.Println(unsafe.Sizeof("")) prints 16. Changing the content of the string doesn't affect the outcome.
Can someone explain how where this number (16) come from?
Strings in Go are represented by reflect.StringHeader containing a pointer to actual string data and a length of string:
type StringHeader struct {
Data uintptr
Len int
}
unsafe.Sizeof(s) will only return the size of StringHeader struct but not the pointed data itself. So (in your example) it will be sum of 8 bytes for Data and 8 bytes for Len making it 16 bytes.

In Python3, the classic mid = start + (end - start) / 2 throws TypeError: list indices must be integers or slices, not float [duplicate]

This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Closed 2 years ago.
In Python3,
The classic binary search first step
mid = start + (end - start) / 2 throws
TypeError: list indices must be integers or slices, not float because division yeilds float
instead of int by default.
Is there a better way to deal with this than doing int(mid)?
In Python 3, the / operator does floating-point division, even if it's between two integers.
You want // instead, which will perform integer division.

Convert a String representation of number to Integer in Java 8+(single line without if) [duplicate]

This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
Closed 4 years ago.
This sounds simple, but I can't find a way to convert a String value(possibly null) to Integer in single line without using if else in Java 8+. The answer might involve usage of ofNullable and isPresent.
Some things I have tried:
String x = ...;
Integer.valueOf(x); // fails if x is null
Optional.ofNullable(Integer.valueOf(x)).orElse(null); // NullPointerException
int value = Optional.ofNullable(x).map(Integer::parseInt).orElse(0);
This will result in a default value of 0 if the input String is null.
As an alternative, use:
Integer value = Optional.ofNullable(x).map(Integer::valueOf).orElse(null);
which will result in null if the input String is null.
What about using ? operator instead of one line if...else?
Integer value = x != null ? Integer.valueOf(x) : null;

how to convert a variable to string in matlab? [duplicate]

This question already has answers here:
print variable-name in Matlab
(4 answers)
Closed 9 years ago.
I have structure array
some_struct_var=struct( 'filed1', filed1, 'filed2', filed2 ,...)
I want to create a string
str=['The struct variable name is :' , some_struct_var]
with the name of the structure variable in it. The some_struct_var may vary and is not fixed.
Create a function that takes any variable as an input and returns the string equivalent of that variable's name as an ouput like so:
varToStr = #(x) inputname(1);
structVarString = varToStr(some_struct_var)
str = ['The struct variable name is :', structVarString]

Resources