Binary numbers
This seems a little odd but for some reason I can only do binary operations in inline assembly blocks.
In the following example the function takes a lowercase character and XORs it with 00100000 to convert it to an uppercase character. Code:
char charupper( char c ) Is there a way to do the same thing with C? Thanks in advance for any advice you can give. |
Re: Binary numbers
c ^ 00100000b
(^ should be bitwise XOR I believe?) /edit: *cough* crap how do you specify binary numbers in C lol o_O whatever: c ^ 32 should do the same thing |
Re: Binary numbers
Yeah that did it, thanks.
I know how to convert from binary to decimal but I was hoping that I wouldn't have to. |
Re: Binary numbers
Meh!
Quote:
Code:
#include <string.h> Why bother with non-portable code ? :) |
Re: Binary numbers
I already know about the standard C library functions and have used that one many times.
I used my function to describe the question better, even still I cannot figure out how to use binary numbers without having to convert them to decimal first. I still think my function uses less instructions though :) |
Re: Binary numbers
Quote:
I suggest that you get used to working with the hexadecimal representation instead of decimal or binary. Why use Hex? http://www.cs.ualberta.ca/~casey/c101/101notes/hex.html Decimal to Hex table http://www.jaworski.com/htmlbook/dec-hex.htm C/C++ has all the operators you need to do bit level operations without resorting to inline asm code. Arithmetic + Binary plus (add) - Binary minus (subtract) * Multiply / Divide % Remainder (modulus) Bitwise << Shift left >> Shift right & Bitwise AND ^ Bitwise XOR (exclusive OR) | Bitwise inclusive OR Logical && Logical AND || Logical OR Assignment = Assignment *= Assign product /= Assign quotient %= Assign remainder (modulus) += Assign sum -= Assign difference <<= Assign left shift >>= Assign right shift &= Assign bitwise AND ^= Assign bitwise XOR |= Assign bitwise OR Relational < Less than > Greater than <= Less than or equal to >= Greater than or equal to == Equal to != Not equal to |
Re: Binary numbers
Thanks for the info, though I know how to use all of the operators I just wondered why I cannot just use a binary number.
I knew that XORing the character with 00100000 would reverse the case. But wouldn't it be easier to just use that binary number without having to convert it first? I have learned both numbering systems already but as I am currently learning x86 assembly binary comes up quite often and if I don't use it every now and again I'll forget it. |
Re: Binary numbers
<background reading>
some C compilers allow this some don't, I don't know about MSVC 6.0... but some compilers might allow binary numbers to be declared like this (e.g. those similar GCC) 0b<binary number> MS c++/c# .Net might allow binarys like this <binary number>B that doesn't work on MSVC 6... :( (we are looking for pre/postfixes on numbers e.g. 0x for hex, 0 for octal 0.0f for float etc, what's the one for binary???) |
Re: Binary numbers
I just want to add that if you want to display number in binary format, you can't do it with printf.
But you can do it with itoa http://www.cplusplus.com/ref/cstdlib/itoa.html also AFAIK you can't specify binary number (like 00101b) I think only 1234, 0x0033 and 07654 works (deciman, hex and octal) IMO it's not in ANSI C/C++ http://www.cs.colorado.edu/~eliuser/c_html/c.html http://www.comnets.rwth-aachen.de/do.../contents.html |
Re: Binary numbers
Quote:
lazy, you can't!!! koraX is korrect! c/c++ doesn't have a way to do this! It has been proposed by the standards committee but as far as I know it hasn't been implemented by any compiler. You have to convert the binary number to octal or hex, first then use the familiar c/c++ CONSTANTS SYNTAX: 0x<hex number> 0<octal number> |
All times are GMT +2. The time now is 07:47. |
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.