Assign octal/hex declared INT/UINT to another variable [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
My WIN32 (C++) code has a UINT lets call it number.
The value of this UINT (or INT doesn't matter) start with a 0 and is recognized as an octal value. It's possible to use the standart operators and the value will keep the octal-system. The same is possible with hex (with foregoing 0x).
The problem is I have to use the Value of number in a buffer to calculate with it without changing the value of number. I can assign a value like 07777 to buffer on declaration line but if use an operation like buffer = number the value in buffer is recognized on decimal base.
Anybody has a solution for me?

There's no such thing in C as an "octal value". Integers are stored in binary.
For example, these three constants:
10
012
0xA
all have exactly the same type and value. They're just different notations -- and the difference exists only in your source code, not at run time. Assigning an octal constant to a variable doesn't make the variable octal.
For example, this:
int n = 012;
stores the value ten in n. You can print that value in any of several formats:
printf("%d\n", n);
printf("0%o\n", n);
printf("0x%x\n", n);
In all three cases, the stored value is converted to a human-readable sequence of characters, in decimal, octal, or hexadecimal.
Anybody has a solution for me?
No, because there is no actual problem.
(Credit goes to juanchopanza for mentioning this in a comment.)

Related

logic behind assign binary literals to an int [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Found that logic on a code and don't get the reason behind that; why use it instead of assign normal int?
(its a character controller in a 3D environment)
// assumes we're not blocked
int blocked = 0x00;
if ( its_a_floor )
blocked |= 0x01;
if ( its_a_wall )
blocked |= 0x02;
0x00 is a "normal int". We are used to base 10 representations, but other than having 10 fingers in total, base 10 is not special. When you use an integer literal in code you can choose between decimal, octal, hexadecimal and binary representation (see here). Don't confuse the value with its representation. 0b01 is the same integers as 1. There is literally no difference in the value.
As a fun fact and to illustrate the above, consider that 0 is actually not a decimal literal. It is an octal literal. It doesn't really matter, because 0 has the same representation in any base.
As the code is using bit-wise operators it would be most convenient to use a binary literals. For example you can see easily that 0b0101 | 0b10101 equals 0b1111 and that 0b0101 & 0b1010 equals 0b0000. This isn't that obvious when using base 10 representations.
However, the code you posted does not use binary literals, but rather hexadecimal literals. This might be due to the fact that binary literals are only standard C++ since C++14, or because programmers used to bit wise operators are so used to hexadecmials, that they still use them rather than the binary.

How does the computer differentiate between the letter and the correct one? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
in c++, When I declare a variable of the type of integer and give it a numerical value, for example, and when printing the variable, its numerical value appears, but when declaring a variable of the character type with the same value of the integer, the return value is a symbol, how is that.
In C and C++ a char is an integer. It is a number, just like short, int, long, etc. One difference is that you don't know if char is signed with a range from -128 to 127, or unsigned with a range from 0 to 255.
However, even though a char is a number, it is usually used to represent an ASCII character value. So, that is what it defaults to. When you write std::cout << 'c' << std::endl; that 'c' is written as a 'c' because that is probably what the programmer wanted to do. To get it to output as a number, you could do std::cout << static_cast<int>('c') << std::endl;
Everything is numbers. Chars are numbers mapped to ASCII (1 byte per character) or UNICODE, which allows to represent almost any character of any language, but using more bytes.
Integers (and floats, longs, etc) are those numbers used for mathematical operations or a representation of themselves.
When you print something to screen the functions printf or cout take numbers (ints or chars) and display them as characters (letters or numbers) through pixel accommodation or a similar mechanism, whose specific dynamic it's not relevant at this point.
Declaring char = 65; it's the same as saying char = 'A'; because at the end of the day the information on a char is stored and processed as a number.

Cannot convert a very large number to hex [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
The community is reviewing whether to reopen this question as of 2 years ago.
Improve this question
I have the following string:
std::string data{"4a00f000f00e1887a9900fff0000004ec00ff004a00f000f00e1887a9900fff0000004ec00ff004a00f000f00e1887a9900fff0000004ec00ff004a00f000f00e1887a9900fff0000004ec00ff000ff004a00f000f00e1887a9900fff000"}
I need to extract it as its equivalent hex value:
4a00f000f00e1887a9900fff0000004ec00ff004a00f000f00e1887a9900fff0000004ec00ff004a00f000f00e1887a9900fff0000004ec00ff004a00f000f00e1887a9900fff0000004ec00ff000ff004a00f000f00e1887a9900fff000
when i do the following code it prints ffffffffffffffff.
I see the issue is the value is too large to fix in value but how do I overcome this?
Is there a way to perhaps put it in a vector bit by bit using a for loop?
{
std::istringstream hex_buffer(data);
unsigned long long value;
hex_buffer >> std::hex >> value;
std::cout << value;
return 0;
}
C++ language uses fixed size integral types. The basic set contains (increasing sizes): char, short, int, long. char has at least 8 bits, short and int at least 16, and long at least 32.
long long is an optional type with at least 64 bits when it exists. But I know no architecture with an integral type of more that 64 bits, meaning that the larger value can be represented in an unsigned long long is 0xFFFFFFFFFFFFFFFF.
You can of course define a class able to handle integer values or arbitrary sized, or use a library that can process arbitrary size values like gmp, by you cannot expect store a number of more than 64 bits in a 64 bit integer.

Why my double is shown as scientific? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
How can I add a scientific and a float number ? I have something like
var1 0.99999899 var2 3.5008552e-05 sum 3.5008552e-05
but what I dont understand is why in the first place var2 is shown as scientific while in the first place I declared
double var1, var2;
so, actually their sum is just var2...
thanks
a
The way a floating-point value is displayed is down to the mechanism by which you display it. It is not a property of the value itself, nor is it in any way stored within the variable:
A number is a number is a number. What you call "scientific" is not a class of numbers. It's a class of representations of numbers. The same way that "twelve" and "12" and "XII" and "a dozen" and "IIIIIIIIIIII" all represent the same number. This "scientific" thing only exists when you decide to represent the number in some specific way (i.e. when you output it). Calculations don't "turn numbers into scientific" the same way that saying that "2 * 6 is twelve" doesn't turn numbers into English words. The variables always store the numbers not the representations. — R. Martinho Fernandes
Your display mechanism — std::cout? — is choosing the best way to output the value. You can override this with IO manipulators such as std::fixed, though it's pretty fiddly sometimes to get it just how you want it, due to library limitations.
If you have <stdio.h> available then you may use int printf ( const char * format, ... ); the format specifier would look something like this: printf("%.5f", your_double) where 5 is the number of digits you want after the decimal point, the default is 6.
(printf docs)

How to determine the number of bits in int [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is what I tried:
int i=-1,size=1;
while (i>>1)
size++;
printf("%d",size);
The goal is to determine the size of int without using the sizeof operator.
The above loop turns out to be infinite. Is there a way to fix it so it does what it is intended to do?
Just use unsigned for i, rather than int. They are
guaranteed to have the same size, and right shift of a signed integer is implementation defined (but will usually shift in the sign bit). And don't forget to divide
the results by CHAR_BIT (which is not guaranteed to be 8).
You have chosen a negative number for right-shifting.
Right shifting a negative number, it gets filled with the sign bit 1 (or not, depending on implementation), so your value can never be 0 (=false), which means you get precisely the infinite loop you are complaining about.
your loop is indeed infinite.
start from i = 1 and shift it left till you reach i =0 and stop. you have the bits.
-edit---
this will work for signed as well as unsigned integer alike.