PDA

View Full Version : Binary numbers


Lazy
02-04-2004, 02:06
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.


char charupper( char c )
{
if ( c & ( 1 << 5 ) ) // Is bit 5 set?
{
__asm // Start inline assembly
{
mov al, c // Move the character into the al register
xor al, 00100000b; // XOR the al register with 00100000 which will invert bit 5
} // End inline assembly
}
else
return c; // If the character was already uppercase, return the same character
}


That function works as expected but is limited to x86 based machines.
Is there a way to do the same thing with C?

Thanks in advance for any advice you can give.

Cheeseh
02-04-2004, 02:12
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

Lazy
02-04-2004, 02:18
Yeah that did it, thanks.
I know how to convert from binary to decimal but I was hoping that I wouldn't have to.

Pierre-Marie Baty
02-04-2004, 05:03
Meh!
In the following example the function takes a lowercase character and XORs it with 00100000 to convert it to an uppercase character.
(...)
Is there a way to do the same thing with C?Sure there is :D

#include <string.h>

char lowerCaseCharacter = 'c';

char upperCaseCharacter = toupper (lowerCaseCharacter);

Works on any architecture, any character set and even unicode. Fast.
Why bother with non-portable code ? :)

Lazy
02-04-2004, 05:07
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 :)

botmeister
02-04-2004, 09:30
I cannot figure out how to use binary numbers without having to convert them to decimal firstAll numbers are stored as binary values inside your computer, therefore a "conversion to decimal" is only a way of thinking about numbers, and not a real conversion to decimal.

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

Lazy
02-04-2004, 09:42
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.

Cheeseh
02-04-2004, 14:31
<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???)

koraX
02-04-2004, 22:49
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/doc/c++std/contents.html

Austin
03-04-2004, 13:09
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.What Lazy is asking, is how do I represent BINARY CONSTANTS in the c/c++ language.

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>

Lazy
03-04-2004, 13:13
Oh well, converting isn't hard at all I just didn't want to do it. Thanks for all of the info.

[ Added ]
koraX, I was displaying a character, not a number in binary format.

@$3.1415rin
03-04-2004, 13:17
with a bit of practice you get used to hex numbers -> no need for binary representation in C/C++

koraX
03-04-2004, 13:23
koraX, I was displaying a character, not a number in binary format.
character is also a number in computer's memory :)

Lazy
03-04-2004, 13:26
I know :)
I thought I had pasted the whole source file and got a little confused.

It was simply printf( "%c\n", charupper( 'z' ) ); but it worked as expected printing out an uppercase Z.