Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Bitwise operation for character

portoon
Beginner
512 Views

Is there any one know that how to do bitwise operation for character?

Following is C++ code

char c;

c = '0';

c <<= 4;

to FORTRAN

character (1) c

c = '0'

c = achar(ishft(iachar(c),+4))

Is this correct ? I don't know, what to do. Please, give me some comments.

Thanks a lot.

0 Kudos
2 Replies
anthonyrichards
New Contributor III
512 Views
Quoting - portoon

Is there any one know that how to do bitwise operation for character?

Following is C++ code

char c;

c = '0';

c <<= 4;

to FORTRAN

character (1) c

c = '0'

c = achar(ishft(iachar(c),+4))

Is this correct ? I don't know, what to do. Please, give me some comments.

Thanks a lot.

Change the sign of the shift argument from +4 to -4. The attached demonstrates what happens by working directly with the integer equivalents to get the same answers (buildit as a console program)

program shiftbits
implicit none

Integer*1 ic, inewc
character*1 c, newc
Equivalence (c,ic), (newc,inewc)

print *, 'Hello World'

c=achar(125) ! ASCII character: close curly bracket, hex code 7D = 011111101

inewc=ishft(ic,-3) ! produces hexcode 00001111 = 0F = ASCII character 'star'
write(6,'(a5,z4,a10, z4)'),"ic = ",c,", inewc = ",newc

print *,"C = ",c,", newc = ",newc

inewc=ishft(ic,-4) ! produces hexcode 00000111 = 07 = ASCII character 'BEL' = beep
write(6,'(a5,z4,a10, z4)'),"ic = ",c,", inewc = ",newc

print *,"C = ",c,", newc = ",newc


newc=achar(ishft(iachar(c),-3)) ! produces the 'star'
print *,"C = ",c,", newc = ",newc

newc=achar(ishft(iachar(c),-4)) ! produces the 'beep'
print *,"C = ",c,", newc = ",newc

end program shiftbits

0 Kudos
portoon
Beginner
512 Views
Quoting - anthonyrichards
Change the sign of the shift argument from +4 to -4. The attached demonstrates what happens by working directly with the integer equivalents to get the same answers (buildit as a console program)

program shiftbits
implicit none

Integer*1 ic, inewc
character*1 c, newc
Equivalence (c,ic), (newc,inewc)

print *, 'Hello World'

c=achar(125) ! ASCII character: close curly bracket, hex code 7D = 011111101

inewc=ishft(ic,-3) ! produces hexcode 00001111 = 0F = ASCII character 'star'
write(6,'(a5,z4,a10, z4)'),"ic = ",c,", inewc = ",newc

print *,"C = ",c,", newc = ",newc

inewc=ishft(ic,-4) ! produces hexcode 00000111 = 07 = ASCII character 'BEL' = beep
write(6,'(a5,z4,a10, z4)'),"ic = ",c,", inewc = ",newc

print *,"C = ",c,", newc = ",newc


newc=achar(ishft(iachar(c),-3)) ! produces the 'star'
print *,"C = ",c,", newc = ",newc

newc=achar(ishft(iachar(c),-4)) ! produces the 'beep'
print *,"C = ",c,", newc = ",newc

end program shiftbits

Thanks anthonyrichards.
I saved several hours for your help.
Have a good day...!!

0 Kudos
Reply