Bitwise operator syntax in C ?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Art
Posts: 642
Joined: Wed Nov 09, 2005 8:01 am

Bitwise operator syntax in C ?

Post by Art »

Hi Guys,
I'm in need of a little help.

Code: Select all

RotateHMKright 							;bitwise rotate HMK array
  rrf 		HMK+0		,F				;a faster way thanx Potter :)
  rrf 		HMK+1		,F				;
  rrf		HMK+2		,F				;went back to rotating right x 79
  rrf		HMK+3		,F				;for rotating left to save ten words
  rrf		HMK+4		,F				;
  rrf		HMK+5		,F				;
  rrf		HMK+6		,F				;
  rrf		HMK+7		,F				;
  rrf		HMK+8		,F				;
  rrf		HMK+9		,F				;
  bcf		HMK+0		,7				;preclear MSB
  btfsc		status		,C				;check carry bit
  bsf		HMK+0		,7				;set MSB
  return							;or return
This is a way to bitwise rotate a 10 byte array called HMK in asm (rr = rotate right).
The routine carries the right most bit to the left most bit of the next
byte, etc. so that if you rotated a 10 byte array 80 times you would end
up with the same values in each byte of the array as you started with,
and if you rotated right 79 times, you would have the same result as
rotating left only once.

I would be happy to find a way to do this in C for rthe PSP since it would
help me improve a lot of verbose routines that I have.
Cheers, Art.
ps. the rrf instructions weren't in an For...Next type loop because it had
to execute as fast as possible.
User avatar
groepaz
Posts: 305
Joined: Thu Sep 01, 2005 7:44 am
Contact:

Post by groepaz »

there is no operator in C that can do this
User avatar
0okm0000
Posts: 116
Joined: Fri Jan 13, 2006 9:51 am
Contact:

Re: Bitwise operator syntax in C ?

Post by 0okm0000 »

Art wrote:Hi Guys,
I'm in need of a little help.

Code: Select all

RotateHMKright 							;bitwise rotate HMK array
  rrf 		HMK+0		,F				;a faster way thanx Potter :)
  rrf 		HMK+1		,F				;
  rrf		HMK+2		,F				;went back to rotating right x 79
  rrf		HMK+3		,F				;for rotating left to save ten words
  rrf		HMK+4		,F				;
  rrf		HMK+5		,F				;
  rrf		HMK+6		,F				;
  rrf		HMK+7		,F				;
  rrf		HMK+8		,F				;
  rrf		HMK+9		,F				;
  bcf		HMK+0		,7				;preclear MSB
  btfsc		status		,C				;check carry bit
  bsf		HMK+0		,7				;set MSB
  return							;or return
This is a way to bitwise rotate a 10 byte array called HMK in asm (rr = rotate right).....
oh
it is MicroChip PIC asm

i m new in C
so the code may be wrong

Code: Select all

	unsigned char HMK[9];
	HMK[9] = 0x55;
	HMK[8] = 0xAA;
	HMK[7] = 0x55;
	HMK[6] = 0xAB;
	HMK[5] = 0x55;
	HMK[4] = 0xAB;
	HMK[3] = 0x55;
	HMK[2] = 0xAA;
	HMK[1] = 0x55;
	HMK[0] = 0xAB;
	printf("\n0x%X%X%X%X%X%X%X%X%X%X", HMK[0], HMK[1], HMK[2], HMK[3], HMK[4], HMK[5], HMK[6], HMK[7], HMK[8], HMK[9]);

	unsigned char temp;
	unsigned int i;
	for&#40;i=0; i<80; i++&#41;
	&#123;
		temp = &#40;HMK&#91;9&#93; << 7&#41;;
		HMK&#91;9&#93; = &#40;HMK&#91;8&#93; << 7&#41; | &#40;HMK&#91;9&#93; >> 1&#41;;
		HMK&#91;8&#93; = &#40;HMK&#91;7&#93; << 7&#41; | &#40;HMK&#91;8&#93; >> 1&#41;;
		HMK&#91;7&#93; = &#40;HMK&#91;6&#93; << 7&#41; | &#40;HMK&#91;7&#93; >> 1&#41;;
		HMK&#91;6&#93; = &#40;HMK&#91;5&#93; << 7&#41; | &#40;HMK&#91;6&#93; >> 1&#41;;
		HMK&#91;5&#93; = &#40;HMK&#91;4&#93; << 7&#41; | &#40;HMK&#91;5&#93; >> 1&#41;;
		HMK&#91;4&#93; = &#40;HMK&#91;3&#93; << 7&#41; | &#40;HMK&#91;4&#93; >> 1&#41;;
		HMK&#91;3&#93; = &#40;HMK&#91;2&#93; << 7&#41; | &#40;HMK&#91;3&#93; >> 1&#41;;
		HMK&#91;2&#93; = &#40;HMK&#91;1&#93; << 7&#41; | &#40;HMK&#91;2&#93; >> 1&#41;;
		HMK&#91;1&#93; = &#40;HMK&#91;0&#93; << 7&#41; | &#40;HMK&#91;1&#93; >> 1&#41;;
		HMK&#91;0&#93; = temp | &#40;HMK&#91;0&#93; >> 1&#41;;
		printf&#40;"\n0x%X%X%X%X%X%X%X%X%X%X", HMK&#91;0&#93;, HMK&#91;1&#93;, HMK&#91;2&#93;, HMK&#91;3&#93;, HMK&#91;4&#93;, HMK&#91;5&#93;, HMK&#91;6&#93;, HMK&#91;7&#93;, HMK&#91;8&#93;, HMK&#91;9&#93;&#41;;
	&#125;
PSP hardware hack
http://0okm.blogspot.com/
User avatar
groepaz
Posts: 305
Joined: Thu Sep 01, 2005 7:44 am
Contact:

Post by groepaz »

if this needs to be as fast as possible, i would suggest inline asm....the C solution above might work (didnt really check, but something simelar will do the trick atleast :=P), but the generated code will probably be far from optimal.

if you want to use C anyway, i'd cast the bitfield to a "unsigned long long" type, and shift those....most likely generates better code than shifting individual bytes.
Art
Posts: 642
Joined: Wed Nov 09, 2005 8:01 am

Post by Art »

It needed to be fast in the pic code where I got the asm example from,
but I just want to learn useable syntax for bitwise operators so that
sample could be implemented in a program in any way possible.
Post Reply